Ticket #8528: ticket8528-nullfilter-1-1-1-final.diff

File ticket8528-nullfilter-1-1-1-final.diff, 3.7 KB (added by marcob, 15 years ago)
  • django/contrib/admin/filterspecs.py

     
    6161            self.lookup_title = f.verbose_name
    6262        rel_name = f.rel.to._meta.pk.name
    6363        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
     64        self.lookup_null_kwarg = '%s__isnull' % (f.name)
    6465        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     66        self.lookup_null = request.GET.get(self.lookup_null_kwarg, None)
    6567        self.lookup_choices = f.get_choices(include_blank=False)
    6668
    6769    def has_output(self):
     
    7173        return self.lookup_title
    7274
    7375    def choices(self, cl):
    74         yield {'selected': self.lookup_val is None,
    75                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
     76        yield {'selected': self.lookup_val is None and not self.lookup_null,
     77               'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_null_kwarg]),
    7678               'display': _('All')}
    7779        for pk_val, val in self.lookup_choices:
    7880            yield {'selected': self.lookup_val == smart_unicode(pk_val),
    79                    'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
     81                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}, [self.lookup_null_kwarg]),
    8082                   'display': val}
     83        if self.field.null:
     84            yield {'selected': self.lookup_null,
     85                   'query_string': cl.get_query_string({self.lookup_null_kwarg: 'True'}, [self.lookup_kwarg]),
     86                   'display': '(%s)' % _("None")}
    8187
    8288FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
    8389
     
    163169    def __init__(self, f, request, params, model, model_admin, field_path=None):
    164170        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    165171        self.lookup_val = request.GET.get(self.field_path, None)
    166         #self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name)
     172        self.lookup_null = request.GET.get(f.name + '__isnull', None)
    167173        self.lookup_choices = model._default_manager.all().distinct().order_by(f.name).values(f.name)
    168174
    169175    def title(self):
    170176        return self.field.verbose_name
    171177
    172178    def choices(self, cl):
    173         yield {'selected': self.lookup_val is None,
    174                'query_string': cl.get_query_string({}, [self.field_path]),
     179        yield {'selected': self.lookup_val is None and not self.lookup_null,
     180               'query_string': cl.get_query_string({}, [self.field.name, self.field_path + '__isnull']),
    175181               'display': _('All')}
    176182        for val in self.lookup_choices:
    177             val = smart_unicode(val[self.field.name])
    178             yield {'selected': self.lookup_val == val,
    179                    'query_string': cl.get_query_string({self.field_path: val}),
    180                    'display': val}
    181 FilterSpec.register(lambda f: True, AllValuesFilterSpec)
     183            if val[self.field_path] is None:
     184                yield {'selected': self.lookup_null,
     185                       'query_string': cl.get_query_string({self.field_path + '__isnull': 'True'}, [self.field_path]),
     186                       'display': '(%s)' % _("None")}
     187            else:
     188                val = smart_unicode(val[self.field_path])
     189                yield {'selected': self.lookup_val == val,
     190                       'query_string': cl.get_query_string({self.field_path: val}, [self.field_path + '__isnull']),
     191                       'display': val}
     192FilterSpec.register(lambda f: True, AllValuesFilterSpec)
Back to Top