Ticket #8528: ticket8528-nullfilter-1-1-1-final.diff
File ticket8528-nullfilter-1-1-1-final.diff, 3.7 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/filterspecs.py
61 61 self.lookup_title = f.verbose_name 62 62 rel_name = f.rel.to._meta.pk.name 63 63 self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name) 64 self.lookup_null_kwarg = '%s__isnull' % (f.name) 64 65 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 66 self.lookup_null = request.GET.get(self.lookup_null_kwarg, None) 65 67 self.lookup_choices = f.get_choices(include_blank=False) 66 68 67 69 def has_output(self): … … 71 73 return self.lookup_title 72 74 73 75 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]), 76 78 'display': _('All')} 77 79 for pk_val, val in self.lookup_choices: 78 80 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]), 80 82 '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")} 81 87 82 88 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) 83 89 … … 163 169 def __init__(self, f, request, params, model, model_admin, field_path=None): 164 170 super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path) 165 171 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) 167 173 self.lookup_choices = model._default_manager.all().distinct().order_by(f.name).values(f.name) 168 174 169 175 def title(self): 170 176 return self.field.verbose_name 171 177 172 178 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']), 175 181 'display': _('All')} 176 182 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} 192 FilterSpec.register(lambda f: True, AllValuesFilterSpec)