Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3096 -- Make admin list_filters respect limit_choices_to.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ae43d11838c783797805774a76cd15ff6ea565ba 1 parent 49ef21d
Karen Tracey authored
5  django/contrib/admin/filterspecs.py
@@ -60,7 +60,7 @@ def __init__(self, f, request, params, model, model_admin):
60 60
             self.lookup_title = f.verbose_name
61 61
         self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)
62 62
         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
63  
-        self.lookup_choices = f.rel.to._default_manager.all()
  63
+        self.lookup_choices = f.get_choices(include_blank=False)
64 64
 
65 65
     def has_output(self):
66 66
         return len(self.lookup_choices) > 1
@@ -72,8 +72,7 @@ def choices(self, cl):
72 72
         yield {'selected': self.lookup_val is None,
73 73
                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
74 74
                'display': _('All')}
75  
-        for val in self.lookup_choices:
76  
-            pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
  75
+        for pk_val, val in self.lookup_choices:
77 76
             yield {'selected': self.lookup_val == smart_unicode(pk_val),
78 77
                    'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
79 78
                    'display': val}
19  tests/regressiontests/admin_views/fixtures/admin-views-colors.xml
... ...
@@ -0,0 +1,19 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<django-objects version="1.0">
  3
+    <object pk="1" model="admin_views.color">
  4
+        <field type="CharField" name="value">Red</field>
  5
+        <field type="BooleanField" name="warm">1</field>
  6
+    </object>
  7
+    <object pk="2" model="admin_views.color">
  8
+        <field type="CharField" name="value">Orange</field>
  9
+        <field type="BooleanField" name="warm">1</field>
  10
+    </object>
  11
+    <object pk="3" model="admin_views.color">
  12
+        <field type="CharField" name="value">Blue</field>
  13
+        <field type="BooleanField" name="warm">0</field>
  14
+    </object>
  15
+    <object pk="4" model="admin_views.color">
  16
+        <field type="CharField" name="value">Green</field>
  17
+        <field type="BooleanField" name="warm">0</field>
  18
+    </object>
  19
+</django-objects>
17  tests/regressiontests/admin_views/models.py
@@ -74,7 +74,24 @@ class ModelWithStringPrimaryKey(models.Model):
74 74
     def __unicode__(self):
75 75
         return self.id
76 76
 
  77
+class Color(models.Model):
  78
+    value = models.CharField(max_length=10)
  79
+    warm = models.BooleanField()   
  80
+    def __unicode__(self):
  81
+        return self.value
  82
+
  83
+class Thing(models.Model):
  84
+    title = models.CharField(max_length=20)
  85
+    color = models.ForeignKey(Color, limit_choices_to={'warm': True})
  86
+    def __unicode__(self):
  87
+        return self.title
  88
+
  89
+class ThingAdmin(admin.ModelAdmin):
  90
+    list_filter = ('color',)
  91
+
77 92
 admin.site.register(Article, ArticleAdmin)
78 93
 admin.site.register(CustomArticle, CustomArticleAdmin)
79 94
 admin.site.register(Section, inlines=[ArticleInline])
80 95
 admin.site.register(ModelWithStringPrimaryKey)
  96
+admin.site.register(Color)
  97
+admin.site.register(Thing, ThingAdmin)
15  tests/regressiontests/admin_views/tests.py
@@ -12,7 +12,7 @@
12 12
 from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
13 13
 
14 14
 class AdminViewBasicTest(TestCase):
15  
-    fixtures = ['admin-views-users.xml']
  15
+    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
16 16
     
17 17
     def setUp(self):
18 18
         self.client.login(username='super', password='secret')
@@ -147,6 +147,19 @@ def testChangeListSortingModelAdmin(self):
147 147
             response.content.index('Middle content') < response.content.index('Newest content'),
148 148
             "Results of sorting on ModelAdmin method are out of order."
149 149
         )
  150
+        
  151
+    def testLimitedFilter(self):
  152
+        """Ensure admin changelist filters do not contain objects excluded via limit_choices_to."""
  153
+        response = self.client.get('/test_admin/admin/admin_views/thing/')
  154
+        self.failUnlessEqual(response.status_code, 200)
  155
+        self.failUnless(
  156
+            '<div id="changelist-filter">' in response.content, 
  157
+            "Expected filter not found in changelist view."
  158
+        )
  159
+        self.failIf(
  160
+            '<a href="?color__id__exact=3">Blue</a>' in response.content,
  161
+            "Changelist filter not correctly limited by limit_choices_to."
  162
+        )
150 163
     
151 164
 def get_perm(Model, perm):
152 165
     """Return the permission object, for the Model"""

0 notes on commit ae43d11

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