Login

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

Author:
Eloff
Posted:
March 18, 2010
Language:
Python
Version:
1.1
Tags:
filter admin null filterspec
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. RelatedNullFilterSpec: django-admin custom filter all/null/not null/choices by Codeko 4 years, 11 months ago
  2. Null Field Admin Filter by bcurtu 6 years, 8 months ago
  3. IsNullFieldListFilter by ssokolow 3 years, 2 months ago
  4. SelectRelatedManager by realmac 3 years, 11 months ago
  5. ActiveManager: filter objects depending on publication and/or expiration dates by haplo 7 years, 2 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.