Skip to content

Instantly share code, notes, and snippets.

@urodoz
Created September 25, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urodoz/5d2fb94bc8e9af74339bd756627afe9f to your computer and use it in GitHub Desktop.
Save urodoz/5d2fb94bc8e9af74339bd756627afe9f to your computer and use it in GitHub Desktop.
Django Null/Not null admin filter
from django.contrib.admin import SimpleListFilter
class NullListFilter(SimpleListFilter):
def lookups(self, request, model_admin):
return (
('1', 'Null',),
('0', '!= Null',),
)
def queryset(self, request, queryset):
if self.value() in ('0', '1'):
kwargs = {'{0}__isnull'.format(self.parameter_name): self.value() == '1'}
return queryset.filter(**kwargs)
return queryset
class StartedNullListFilter(NullListFilter):
title = u'Started'
parameter_name = u'started'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment