Ticket #14467: AllValuesFilterSpec_Null.2.diff
File AllValuesFilterSpec_Null.2.diff, 5.3 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/filterspecs.py
161 161 class AllValuesFilterSpec(FilterSpec): 162 162 def __init__(self, f, request, params, model, model_admin): 163 163 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) 165 168 self.lookup_choices = model_admin.queryset(request).distinct().order_by(f.name).values(f.name) 166 169 167 170 def title(self): 168 171 return self.field.verbose_name 169 172 170 173 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]), 173 176 'display': _('All')} 177 178 include_none = False 174 179 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} 180 val = val[self.field.name] 181 if val is None: 182 include_none = True 183 else: 184 val = smart_unicode(val) 185 yield {'selected': self.lookup_val == val, 186 'query_string': cl.get_query_string({self.lookup_kwarg: val}, [self.lookup_kwarg2]), 187 'display': val} 188 if include_none: 189 yield {'selected': self.lookup_val2 is not None, 190 'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]), 191 'display': _('None')} 179 192 FilterSpec.register(lambda f: True, AllValuesFilterSpec) -
tests/regressiontests/admin_filterspecs/tests.py
1 from django.test import TestCase 2 3 from django.contrib import admin 4 from django.contrib.admin.views.main import ChangeList 5 from django.contrib.admin.options import IncorrectLookupParameters 6 7 from models import OptionalAge 8 9 class AllValuesFilterSpecTest(TestCase): 10 11 def setUp(self): 12 OptionalAge.objects.create(name='Sam', age=35) 13 OptionalAge.objects.create(name='Tilk', age=135) 14 OptionalAge.objects.create(name='Oneill', age=None) 15 16 def testAllValuesFilterSpecTest(self): 17 18 m = OptionalAgeAdmin(OptionalAge, admin.site) 19 20 r = MockRequest() 21 r.GET = {'age__isnull': 'True'} 22 23 cl = ChangeList(r, OptionalAge, m.list_display, m.list_display_links, 24 m.list_filter, m.date_hierarchy, m.search_fields, 25 m.list_select_related, m.list_per_page, m.list_editable, m) 26 27 """ make sure cl.get_query_set does not raise IncorrectLookupParameters """ 28 29 queryset = cl.get_query_set() 30 31 filterspec = cl.get_filters(r)[0][0] 32 33 choices = list(filterspec.choices(cl)) 34 35 """ make sure the last choice is None and is selected """ 36 37 self.assertEquals(choices[3]['selected'], True) 38 self.assertEquals(choices[3]['query_string'], '?age__isnull=True') 39 40 r = MockRequest() 41 r.GET = {'age': '135'} 42 43 cl = ChangeList(r, OptionalAge, m.list_display, m.list_display_links, 44 m.list_filter, m.date_hierarchy, m.search_fields, 45 m.list_select_related, m.list_per_page, m.list_editable, m) 46 47 filterspec = cl.get_filters(r)[0][0] 48 49 choices = list(filterspec.choices(cl)) 50 51 """ make sure correct choice is selected """ 52 53 self.assertEquals(choices[2]['selected'], True) 54 self.assertEquals(choices[2]['query_string'], '?age=135') 55 56 def tearDown(self): 57 OptionalAge.objects.all().delete() 58 59 class OptionalAgeAdmin(admin.ModelAdmin): 60 list_filter = ('age',) 61 order_by = '-id' 62 63 class MockRequest(object): 64 GET = {} 65 No newline at end of file -
tests/regressiontests/admin_filterspecs/models.py
1 from django.db import models 2 3 class OptionalAge(models.Model): 4 name = models.CharField(max_length=25) 5 age = models.PositiveIntegerField(null=True, blank=True)