IsNullFieldListFilter

 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
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import FieldListFilter

class IsNullFieldListFilter(FieldListFilter):
    def __init__(self, field, request, params, model, model_admin, field_path):
        self.lookup_kwarg = '%s__isnull' % field_path
        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
        super(IsNullFieldListFilter, self).__init__(field,
            request, params, model, model_admin, field_path)

    def expected_parameters(self):
        return [self.lookup_kwarg]

    def choices(self, cl):
        for lookup, title in (
                (None, _('All')),
                ('False', _('Yes')),
                ('True', _('No'))):
            yield {
                'selected': self.lookup_val == lookup,
                'query_string': cl.get_query_string({
                        self.lookup_kwarg: lookup,
                    }),
                'display': title,
            }

More like this

  1. Closure for FieldListFilter classes with custom sets of ranges by ssokolow 11 months, 4 weeks ago
  2. restrict user access to modeladmin via metaclass by code_shogan 2 years, 3 months ago
  3. Dynamically insert or append a value to an admin option, e.g. list_display or list_filter by frankban 1 year, 10 months ago
  4. django-admin custom filter: IS NULL/IS NOT NULL by Eloff 3 years, 3 months ago
  5. Filter by taggit tags in the admin by flupke 1 year, 8 months ago

Comments

RobertPattinson (on August 2, 2012):

As the title does a pretty good job of condensing, this is a subclass of FieldListFilter for the Django 1.4 Admin system thanks adobe.com

#

RobertPattinson (on August 6, 2012):

if you had an Author model and wanted to filter it by whether authors were also users of the site, you could add this to your AuthorAdmin class:

list_filter = ( ('user_acct', IsNullFieldListFilter), ) Thanks Samsung Galaxy Beam cases

#

(Forgotten your password?)