Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19318 -- Ensured that the admin's SimpleListFilter options can…

… be displayed as selected even if the lookup's first element is not a string.
  • Loading branch information...
commit 88e17156393be86df64d44554de8ab77a6d3ed7d 1 parent 5fa5621
Sebastián Magrí authored November 18, 2012 jphalip committed December 03, 2012
4  django/contrib/admin/filters.py
@@ -9,7 +9,7 @@
9 9
 
10 10
 from django.db import models
11 11
 from django.core.exceptions import ImproperlyConfigured, ValidationError
12  
-from django.utils.encoding import smart_text
  12
+from django.utils.encoding import smart_text, force_text
13 13
 from django.utils.translation import ugettext_lazy as _
14 14
 from django.utils import timezone
15 15
 from django.contrib.admin.util import (get_model_from_relation,
@@ -102,7 +102,7 @@ def choices(self, cl):
102 102
         }
103 103
         for lookup, title in self.lookup_choices:
104 104
             yield {
105  
-                'selected': self.value() == lookup,
  105
+                'selected': self.value() == force_text(lookup),
106 106
                 'query_string': cl.get_query_string({
107 107
                     self.parameter_name: lookup,
108 108
                 }, []),
58  tests/regressiontests/admin_filters/tests.py
@@ -78,6 +78,21 @@ class DecadeListFilterParameterEndsWith__Isnull(DecadeListFilter):
78 78
     parameter_name = 'decade__isnull' # Ends with '__isnull"
79 79
 
80 80
 
  81
+class DepartmentListFilterLookupWithNonStringValue(SimpleListFilter):
  82
+    title = 'department'
  83
+    parameter_name = 'department'
  84
+
  85
+    def lookups(self, request, model_admin):
  86
+        return set([
  87
+            (employee.department.id,  # Intentionally not a string (Refs #19318)
  88
+             employee.department.code)
  89
+            for employee in model_admin.queryset(request).all()
  90
+        ])
  91
+
  92
+    def queryset(self, request, queryset):
  93
+        if self.value():
  94
+            return queryset.filter(department__id=self.value())
  95
+
81 96
 class CustomUserAdmin(UserAdmin):
82 97
     list_filter = ('books_authored', 'books_contributed')
83 98
 
@@ -118,6 +133,10 @@ class EmployeeAdmin(ModelAdmin):
118 133
     list_filter = ['department']
119 134
 
120 135
 
  136
+class DepartmentFilterEmployeeAdmin(EmployeeAdmin):
  137
+    list_filter = [DepartmentListFilterLookupWithNonStringValue, ]
  138
+
  139
+
121 140
 class ListFiltersTests(TestCase):
122 141
 
123 142
     def setUp(self):
@@ -140,6 +159,14 @@ def setUp(self):
140 159
         self.gipsy_book.contributors = [self.bob, self.lisa]
141 160
         self.gipsy_book.save()
142 161
 
  162
+        # Departments
  163
+        self.dev = Department.objects.create(code='DEV', description='Development')
  164
+        self.design = Department.objects.create(code='DSN', description='Design')
  165
+
  166
+        # Employees
  167
+        self.john = Employee.objects.create(name='John Blue', department=self.dev)
  168
+        self.jack = Employee.objects.create(name='Jack Red', department=self.design)
  169
+
143 170
     def get_changelist(self, request, model, modeladmin):
144 171
         return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links,
145 172
             modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
@@ -638,6 +665,28 @@ def test_parameter_ends_with__in__or__isnull(self):
638 665
         self.assertEqual(choices[2]['selected'], True)
639 666
         self.assertEqual(choices[2]['query_string'], '?decade__isnull=the+90s')
640 667
 
  668
+    def test_lookup_with_non_string_value(self):
  669
+        """
  670
+        Ensure choices are set the selected class when using non-string values
  671
+        for lookups in SimpleListFilters.
  672
+        Refs #19318
  673
+        """
  674
+
  675
+        modeladmin = DepartmentFilterEmployeeAdmin(Employee, site)
  676
+        request = self.request_factory.get('/', {'department': '1'})
  677
+        changelist = self.get_changelist(request, Employee, modeladmin)
  678
+
  679
+        queryset = changelist.get_query_set(request)
  680
+
  681
+        self.assertEqual(list(queryset), [self.john])
  682
+
  683
+        filterspec = changelist.get_filters(request)[0][-1]
  684
+        self.assertEqual(force_text(filterspec.title), 'department')
  685
+        choices = list(filterspec.choices(changelist))
  686
+        self.assertEqual(choices[2]['display'], 'DEV')
  687
+        self.assertEqual(choices[2]['selected'], True)
  688
+        self.assertEqual(choices[2]['query_string'], '?department=1')
  689
+
641 690
     def test_fk_with_to_field(self):
642 691
         """
643 692
         Ensure that a filter on a FK respects the FK's to_field attribute.
@@ -645,17 +694,12 @@ def test_fk_with_to_field(self):
645 694
         """
646 695
         modeladmin = EmployeeAdmin(Employee, site)
647 696
 
648  
-        dev = Department.objects.create(code='DEV', description='Development')
649  
-        design = Department.objects.create(code='DSN', description='Design')
650  
-        john = Employee.objects.create(name='John Blue', department=dev)
651  
-        jack = Employee.objects.create(name='Jack Red', department=design)
652  
-
653 697
         request = self.request_factory.get('/', {})
654 698
         changelist = self.get_changelist(request, Employee, modeladmin)
655 699
 
656 700
         # Make sure the correct queryset is returned
657 701
         queryset = changelist.get_query_set(request)
658  
-        self.assertEqual(list(queryset), [jack, john])
  702
+        self.assertEqual(list(queryset), [self.jack, self.john])
659 703
 
660 704
         filterspec = changelist.get_filters(request)[0][-1]
661 705
         self.assertEqual(force_text(filterspec.title), 'department')
@@ -680,7 +724,7 @@ def test_fk_with_to_field(self):
680 724
 
681 725
         # Make sure the correct queryset is returned
682 726
         queryset = changelist.get_query_set(request)
683  
-        self.assertEqual(list(queryset), [john])
  727
+        self.assertEqual(list(queryset), [self.john])
684 728
 
685 729
         filterspec = changelist.get_filters(request)[0][-1]
686 730
         self.assertEqual(force_text(filterspec.title), 'department')

0 notes on commit 88e1715

Please sign in to comment.
Something went wrong with that request. Please try again.