Login

Null Field Admin Filter

Author:
bcurtu
Posted:
December 30, 2008
Language:
Python
Version:
1.0
Tags:
filter admin null
Score:
1 (after 1 ratings)

This patch adds a new admin Filter, for Filtering nullable fields. It adds 3 possible choices: 'All' (no filter), 'Null' (it applies field__isnull=True filter), and 'With Value' (it filters null values).

This patch is interesting when you have a Integer or String fields and you want to filter wether a value is set or not. In other case, it would show too many filtering options.

Remember this is a patch and you must modify a django file in django/contrib/admin/filterspecs.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Index: /home/bcm/wsp/www/django/contrib/admin/filterspecs.py
===================================================================
--- /home/bcm/wsp/www/django/contrib/admin/filterspecs.py	(revision 1815)
+++ /home/bcm/wsp/www/django/contrib/admin/filterspecs.py	(working copy)
@@ -155,6 +155,24 @@
 
 FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)
 
+
+class NullFilterSpec(FilterSpec):
+    def __init__(self, f, request, params, model, model_admin):
+        super(NullFilterSpec, self).__init__(f, request, params, model, model_admin)
+        self.lookup_kwarg = '%s__isnull' % f.name
+        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
+
+    def choices(self, cl):
+        yield {'selected': self.lookup_val is None,
+               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
+               'display': _('All')}
+        for k, v in ((True,_('Null')),('',_('With value'))):
+            yield {'selected': k == self.lookup_val,
+                    'query_string': cl.get_query_string({self.lookup_kwarg: k}),
+                    'display': v}
+FilterSpec.register(lambda f: f.null, NullFilterSpec)
+
+
 # This should be registered last, because it's a last resort. For example,
 # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
 # more appropriate, and the AllValuesFilterSpec won't get used for it.

More like this

  1. RelatedNullFilterSpec: django-admin custom filter all/null/not null/choices by Codeko 4 years, 4 months ago
  2. django-admin custom filter: IS NULL/IS NOT NULL by Eloff 4 years, 11 months ago
  3. IsNullFieldListFilter by ssokolow 2 years, 8 months ago
  4. Django Admin Filter __in query string by justhamade 1 year, 10 months ago
  5. Updated FileField / ImageField with a delete checkbox by tomZ 6 years, 11 months ago

Comments

Ciantic (on March 19, 2009):

1. There is slight bug (Null is not shown to be selected without this change):

yield {'selected': k == self.lookup_val,

Change to:

yield {'selected': str(k) == str(self.lookup_val),

2. One can use this without editing django, just create file for this, I call it filterspecs.py in my extrafilters module.

Then in the end of your filterspecs.py just do:

FilterSpec.filter_specs.insert(0, (lambda f: f.null, NullFilterSpec))

#

Please login first before commenting.