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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
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.