Skip to content

Instantly share code, notes, and snippets.

@jaymzcd
Created January 20, 2011 15:47
Show Gist options
  • Select an option

  • Save jaymzcd/788077 to your computer and use it in GitHub Desktop.

Select an option

Save jaymzcd/788077 to your computer and use it in GitHub Desktop.
A filterspec to attach to your models for showing __isnull fields
class NoneFilterSpec(ChoicesFilterSpec):
"""Filters based on a field being either None or populated """
def __init__(self, f, request, params, model, model_admin):
super(NoneFilterSpec, 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)
self.lookup_choices = [True, False]
self.lookup_choices.sort()
def choices(self, clist):
yield {
'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')
}
for val in self.lookup_choices:
yield {
'selected': smart_unicode(val) == self.lookup_val,
'query_string': clist.get_query_string({self.lookup_kwarg: val}),
'display': repr(val)
}
def title(self):
return _('%(field_name)s that is empty') % {'field_name': self.field.verbose_name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment