Login

django-admin custom filter: IS NULL/IS NOT NULL

Author:
Eloff
Posted:
March 18, 2010
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

A simple django-admin filter to allow a boolean-like filter for IS NULL/IS NOT NULL.

By default it applies to CharField, IntegerField, and FileField, but you can change this by editing NullFilterSpec.fields.

 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
30
31
32
33
34
35
36
from django.contrib.admin.filterspecs import FilterSpec
from django.db import models
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

# FilterSpec.register places the new FilterSpec at the back
# of the list. This can be a problem, because the first 
# matching FilterSpec is the one used.
def _register_front(cls, test, factory):
    cls.filter_specs.insert(0, (test, factory))

FilterSpec.register_front = classmethod(_register_front)

class NullFilterSpec(FilterSpec):
    fields = (models.CharField, models.IntegerField, models.FileField)
    
    #@classmethod
    def test(cls, field):
        return field.null and isinstance(field, cls.fields) and not field._choices
    test = classmethod(test)
    
    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):
        # bool(v) must be False for IS NOT NULL and True for IS NULL, but can only be a string
        for k, v in ((_('All'), None), (_('Has value'), ''), (_('Omitted'), '1')):
            yield {
                'selected' : self.lookup_val == v,
                'query_string' : cl.get_query_string({self.lookup_kwarg : v}),
                'display' : k
            }
    
FilterSpec.register_front(NullFilterSpec.test, NullFilterSpec)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months 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, 7 months ago

Comments

lauris (on June 23, 2010):

Really usefull filter, thanks. It's a bit buggy with ManyToManyField though - for.ex. if an object refers to two other objects, it is shown two times in the filtered list.

#

Please login first before commenting.