Ticket #14467: AllValuesFilterSpec_Null.diff

File AllValuesFilterSpec_Null.diff, 2.0 KB (added by oyvind, 14 years ago)

Proper fix

  • django/contrib/admin/filterspecs.py

     
    161161class AllValuesFilterSpec(FilterSpec):
    162162    def __init__(self, f, request, params, model, model_admin):
    163163        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin)
    164         self.lookup_val = request.GET.get(f.name, None)
     164        self.lookup_kwarg = f.name
     165        self.lookup_kwarg2 = '%s__isnull' % f.name
     166        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     167        self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
    165168        self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
    166169
    167170    def title(self):
    168171        return self.field.verbose_name
    169172
    170173    def choices(self, cl):
    171         yield {'selected': self.lookup_val is None,
    172                'query_string': cl.get_query_string({}, [self.field.name]),
     174        yield {'selected': self.lookup_val is None and self.lookup_val2 is None,
     175               'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg2]),
    173176               'display': _('All')}
    174177        for val in self.lookup_choices:
    175             val = smart_unicode(val[self.field.name])
    176             yield {'selected': self.lookup_val == val,
    177                    'query_string': cl.get_query_string({self.field.name: val}),
    178                    'display': val}
     178            val = val[self.field.name]
     179            if val is None:
     180                val = smart_unicode(val)
     181                yield {'selected': self.lookup_val2 is not None,
     182                    'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
     183                    'display': val}
     184            else:
     185                val = smart_unicode(val)
     186                yield {'selected': self.lookup_val2 == val,
     187                       'query_string': cl.get_query_string({self.lookup_kwarg: val}, [self.lookup_kwarg2]),
     188                       'display': val}
    179189FilterSpec.register(lambda f: True, AllValuesFilterSpec)
Back to Top