Login

IsNullFieldListFilter

Author:
ssokolow
Posted:
June 23, 2012
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

As the title does a pretty good job of condensing, this is a subclass of FieldListFilter for the Django 1.4 Admin system which allows you filter by whether a field is or is not NULL.

For example, 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),
)

For the record, it began life as a modified version of BooleanFieldListFilter from django.contrib.admin.filters.

 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. codigo alto nivel by MrRocklion 1 week, 1 day ago
  2. Load template from specific app by Krzysiek555 4 weeks ago
  3. PostgreSQL JSON subqueries by dolamroth 1 month ago
  4. "Magic Link" Management Command by webology 6 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 6 months ago

Comments

Please login first before commenting.