Ticket #8528: 8528_filterspec_null.diff

File 8528_filterspec_null.diff, 11.9 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/admin/filterspecs.py

    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    index eab5407..a18a6e5 100644
    a b class RelatedFilterSpec(FilterSpec):  
    7676            self.lookup_title = f.verbose_name # use field name
    7777        rel_name = other_model._meta.pk.name
    7878        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
     79        self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
    7980        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     81        self.lookup_val_isnull = bool(request.GET.get(self.lookup_kwarg_isnull, None))
    8082        self.lookup_choices = f.get_choices(include_blank=False)
    8183
    8284    def has_output(self):
    class RelatedFilterSpec(FilterSpec):  
    8688        return self.lookup_title
    8789
    8890    def choices(self, cl):
    89         yield {'selected': self.lookup_val is None,
    90                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
     91        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
     92        yield {'selected': self.lookup_val is None and not self.lookup_val_isnull,
     93               'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
    9194               'display': _('All')}
    9295        for pk_val, val in self.lookup_choices:
    9396            yield {'selected': self.lookup_val == smart_unicode(pk_val),
    94                    'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
     97                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}, [self.lookup_kwarg_isnull]),
    9598                   'display': val}
     99        if hasattr(self.field, 'rel') and self.field.null:
     100            yield {'selected': self.lookup_val_isnull,
     101                   'query_string': cl.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
     102                   'display': EMPTY_CHANGELIST_VALUE}
    96103
    97104FilterSpec.register(lambda f: (
    98105        hasattr(f, 'rel') and bool(f.rel) or
    class AllValuesFilterSpec(FilterSpec):  
    192199        super(AllValuesFilterSpec, self).__init__(f, request, params, model,
    193200                                                  model_admin,
    194201                                                  field_path=field_path)
    195         self.lookup_val = request.GET.get(self.field_path, None)
    196         parent_model, reverse_path = reverse_field_path(model, field_path)
     202        self.lookup_kwarg = self.field_path
     203        self.lookup_kwarg_isnull = '%s__isnull' % self.field_path
     204        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     205        self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None)
     206        parent_model, reverse_path = reverse_field_path(model, self.field_path)
    197207        queryset = parent_model._default_manager.all()
    198208        # optional feature: limit choices base on existing relationships
    199209        # queryset = queryset.complex_filter(
    class AllValuesFilterSpec(FilterSpec):  
    202212        queryset = queryset.filter(limit_choices_to)
    203213
    204214        self.lookup_choices = \
    205             queryset.distinct().order_by(f.name).values(f.name)
     215            queryset.distinct().order_by(f.name).values_list(f.name, flat=True)
    206216
    207217    def title(self):
    208218        return self.field.verbose_name
    209219
    210220    def choices(self, cl):
    211         yield {'selected': self.lookup_val is None,
    212                'query_string': cl.get_query_string({}, [self.field_path]),
     221        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
     222        yield {'selected': self.lookup_val is None and self.lookup_val_isnull is None,
     223               'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
    213224               'display': _('All')}
     225        include_none = False
     226       
    214227        for val in self.lookup_choices:
    215             val = smart_unicode(val[self.field.name])
     228            if val is None:
     229                include_none = True
     230                continue
     231            val = smart_unicode(val)
     232           
    216233            yield {'selected': self.lookup_val == val,
    217                    'query_string': cl.get_query_string({self.field_path: val}),
     234                   'query_string': cl.get_query_string({self.lookup_kwarg: val}, [self.lookup_kwarg_isnull]),
    218235                   'display': val}
     236        if include_none:
     237            yield {'selected': self.lookup_val_isnull is not None,
     238                    'query_string': cl.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
     239                    'display': EMPTY_CHANGELIST_VALUE}
     240
    219241FilterSpec.register(lambda f: True, AllValuesFilterSpec)
  • new file tests/regressiontests/admin_filterspecs/models.py

    diff --git a/tests/regressiontests/admin_filterspecs/__init__.py b/tests/regressiontests/admin_filterspecs/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/admin_filterspecs/models.py b/tests/regressiontests/admin_filterspecs/models.py
    new file mode 100644
    index 0000000..e62a070
    - +  
     1from django.db import models
     2from django.contrib.auth.models import User
     3
     4class Book(models.Model):
     5    title = models.CharField(max_length=25)
     6    year = models.PositiveIntegerField(null=True, blank=True)
     7    author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True)
     8    contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)
  • new file tests/regressiontests/admin_filterspecs/tests.py

    diff --git a/tests/regressiontests/admin_filterspecs/tests.py b/tests/regressiontests/admin_filterspecs/tests.py
    new file mode 100644
    index 0000000..e8be8cf
    - +  
     1from django.test import TestCase
     2from django.test.client import RequestFactory
     3from django.contrib.auth.models import User
     4
     5from django.contrib import admin
     6from django.contrib.admin.views.main import ChangeList
     7
     8from models import Book
     9
     10class FilterSpecsTests(TestCase):
     11   
     12    def setUp(self):
     13        # Users
     14        alfred = User.objects.create_user('alfred', 'alfred@example.com')
     15        bob = User.objects.create_user('bob', 'alfred@example.com')
     16        lisa = User.objects.create_user('lisa', 'lisa@example.com')
     17       
     18        #Books
     19        bio_book = Book.objects.create(title='Django: a biography', year=1999, author=alfred)
     20        django_book = Book.objects.create(title='The Django Book', year=None, author=bob)
     21        gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002)
     22        gipsy_book.contributors = [bob, lisa]
     23        gipsy_book.save()
     24       
     25        self.request_factory = RequestFactory()
     26       
     27    def test_AllValuesFilterSpec(self):
     28        modeladmin = BookAdmin(Book, admin.site)
     29       
     30        request = self.request_factory.get('/', {'year__isnull': 'True'})
     31        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     32            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     33            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     34           
     35        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
     36        queryset = changelist.get_query_set()
     37
     38        # Make sure the last choice is None and is selected
     39        filterspec = changelist.get_filters(request)[0][0]
     40        self.assertEquals(filterspec.title(), u'year')
     41        choices = list(filterspec.choices(changelist))
     42        self.assertEquals(choices[3]['selected'], True)
     43        self.assertEquals(choices[3]['query_string'], '?year__isnull=True')
     44       
     45        request = self.request_factory.get('/', {'year': '2002'})
     46        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     47            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     48            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     49           
     50        # Make sure the correct choice is selected   
     51        filterspec = changelist.get_filters(request)[0][0]
     52        self.assertEquals(filterspec.title(), u'year')
     53        choices = list(filterspec.choices(changelist))
     54        self.assertEquals(choices[2]['selected'], True)
     55        self.assertEquals(choices[2]['query_string'], '?year=2002')
     56       
     57    def test_RelatedFilterSpec_ForeignKey(self):
     58        modeladmin = BookAdmin(Book, admin.site)
     59       
     60        request = self.request_factory.get('/', {'author__isnull': 'True'})
     61        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     62            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     63            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     64       
     65        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
     66        queryset = changelist.get_query_set()
     67
     68        # Make sure the last choice is None and is selected
     69        filterspec = changelist.get_filters(request)[0][1]
     70        self.assertEquals(filterspec.title(), u'author')
     71        choices = list(filterspec.choices(changelist))
     72        self.assertEquals(choices[4]['selected'], True)
     73        self.assertEquals(choices[4]['query_string'], '?author__isnull=True')
     74       
     75        request = self.request_factory.get('/', {'author__id__exact': '1'})
     76        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     77            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     78            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     79           
     80        # Make sure the correct choice is selected
     81        filterspec = changelist.get_filters(request)[0][1]
     82        self.assertEquals(filterspec.title(), u'author')
     83        choices = list(filterspec.choices(changelist))
     84        self.assertEquals(choices[1]['selected'], True)
     85        self.assertEquals(choices[1]['query_string'], '?author__id__exact=1')
     86       
     87    def test_RelatedFilterSpec_ManyToMany(self):
     88        modeladmin = BookAdmin(Book, admin.site)
     89       
     90        request = self.request_factory.get('/', {'contributors__isnull': 'True'})
     91        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     92            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     93            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     94       
     95        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
     96        queryset = changelist.get_query_set()
     97
     98        # Make sure the last choice is None and is selected
     99        filterspec = changelist.get_filters(request)[0][2]
     100        self.assertEquals(filterspec.title(), u'user')
     101        choices = list(filterspec.choices(changelist))
     102        self.assertEquals(choices[4]['selected'], True)
     103        self.assertEquals(choices[4]['query_string'], '?contributors__isnull=True')
     104       
     105        request = self.request_factory.get('/', {'contributors__id__exact': '2'})
     106        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
     107            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
     108            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
     109           
     110        # Make sure the correct choice is selected
     111        filterspec = changelist.get_filters(request)[0][2]
     112        self.assertEquals(filterspec.title(), u'user')
     113        choices = list(filterspec.choices(changelist))
     114        self.assertEquals(choices[2]['selected'], True)
     115        self.assertEquals(choices[2]['query_string'], '?contributors__id__exact=2')
     116
     117class BookAdmin(admin.ModelAdmin):
     118    list_filter = ('year', 'author', 'contributors')
     119    order_by = '-id'
Back to Top