Login

Null Field Admin Filter

Author:
bcurtu
Posted:
December 30, 2008
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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.