Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.X] Fixed #3096 -- Make admin list_filters respect limit_choices_to.

Backport of [9241] from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 118383c6071a55683a1789dcd6f80bf979ac83da 1 parent bd60c52
Karen Tracey kmtracey authored
5 django/contrib/admin/filterspecs.py
View
@@ -60,7 +60,7 @@ def __init__(self, f, request, params, model, model_admin):
self.lookup_title = f.verbose_name
self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- self.lookup_choices = f.rel.to._default_manager.all()
+ self.lookup_choices = f.get_choices(include_blank=False)
def has_output(self):
return len(self.lookup_choices) > 1
@@ -72,8 +72,7 @@ def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')}
- for val in self.lookup_choices:
- pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
+ for pk_val, val in self.lookup_choices:
yield {'selected': self.lookup_val == smart_unicode(pk_val),
'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
'display': val}
19 tests/regressiontests/admin_views/fixtures/admin-views-colors.xml
View
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+ <object pk="1" model="admin_views.color">
+ <field type="CharField" name="value">Red</field>
+ <field type="BooleanField" name="warm">1</field>
+ </object>
+ <object pk="2" model="admin_views.color">
+ <field type="CharField" name="value">Orange</field>
+ <field type="BooleanField" name="warm">1</field>
+ </object>
+ <object pk="3" model="admin_views.color">
+ <field type="CharField" name="value">Blue</field>
+ <field type="BooleanField" name="warm">0</field>
+ </object>
+ <object pk="4" model="admin_views.color">
+ <field type="CharField" name="value">Green</field>
+ <field type="BooleanField" name="warm">0</field>
+ </object>
+</django-objects>
17 tests/regressiontests/admin_views/models.py
View
@@ -74,7 +74,24 @@ class ModelWithStringPrimaryKey(models.Model):
def __unicode__(self):
return self.id
+class Color(models.Model):
+ value = models.CharField(max_length=10)
+ warm = models.BooleanField()
+ def __unicode__(self):
+ return self.value
+
+class Thing(models.Model):
+ title = models.CharField(max_length=20)
+ color = models.ForeignKey(Color, limit_choices_to={'warm': True})
+ def __unicode__(self):
+ return self.title
+
+class ThingAdmin(admin.ModelAdmin):
+ list_filter = ('color',)
+
admin.site.register(Article, ArticleAdmin)
admin.site.register(CustomArticle, CustomArticleAdmin)
admin.site.register(Section, inlines=[ArticleInline])
admin.site.register(ModelWithStringPrimaryKey)
+admin.site.register(Color)
+admin.site.register(Thing, ThingAdmin)
15 tests/regressiontests/admin_views/tests.py
View
@@ -12,7 +12,7 @@
from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
class AdminViewBasicTest(TestCase):
- fixtures = ['admin-views-users.xml']
+ fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
def setUp(self):
self.client.login(username='super', password='secret')
@@ -147,6 +147,19 @@ def testChangeListSortingModelAdmin(self):
response.content.index('Middle content') < response.content.index('Newest content'),
"Results of sorting on ModelAdmin method are out of order."
)
+
+ def testLimitedFilter(self):
+ """Ensure admin changelist filters do not contain objects excluded via limit_choices_to."""
+ response = self.client.get('/test_admin/admin/admin_views/thing/')
+ self.failUnlessEqual(response.status_code, 200)
+ self.failUnless(
+ '<div id="changelist-filter">' in response.content,
+ "Expected filter not found in changelist view."
+ )
+ self.failIf(
+ '<a href="?color__id__exact=3">Blue</a>' in response.content,
+ "Changelist filter not correctly limited by limit_choices_to."
+ )
def get_perm(Model, perm):
"""Return the permission object, for the Model"""
Please sign in to comment.
Something went wrong with that request. Please try again.