Ticket #5273: 5273.patch

File 5273.patch, 2.3 KB (added by Yuri Baburov <burchik@…>, 8 years ago)

patch

  • D:/web/projects/django-newforms/django/contrib/admin/filterspecs.py

     
    5959        self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)
    6060        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    6161        self.lookup_choices = f.rel.to._default_manager.all()
     62        #lookup_none_kwarg defined only on nullable fields, empty string otherwise
     63        self.lookup_none_kwarg  = (f.null or '') and '%s__%s__isnull' % (f.name, f.rel.to._meta.pk.name)
     64        self.lookup_none_val = request.GET.get(self.lookup_none_kwarg, None)
    6265
    6366    def has_output(self):
    6467        return len(self.lookup_choices) > 1
     
    6770        return self.lookup_title
    6871
    6972    def choices(self, cl):
    70         yield {'selected': self.lookup_val is None,
    71                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
     73        yield {'selected': self.lookup_val is None and self.lookup_none_val is None,
     74               'query_string': cl.get_query_string({}, [self.lookup_kwarg,
     75                                                        self.lookup_none_kwarg]),
    7276               'display': _('All')}
    7377        for val in self.lookup_choices:
    7478            pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
     
    7377        for val in self.lookup_choices:
    7478            pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
    7579            yield {'selected': self.lookup_val == smart_unicode(pk_val),
    76                    'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
     80                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val},
     81                                                       [self.lookup_none_kwarg]),
    7782                   'display': val}
     83        if self.lookup_none_kwarg:
     84            yield {'selected': self.lookup_none_val != None,
     85                   'query_string': cl.get_query_string({self.lookup_none_kwarg: True},
     86                                                       [self.lookup_kwarg]),
     87                   'display': _('None')}
    7888
    7989FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
Back to Top