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
This commit is contained in:
parent
49ef21d9ea
commit
ae43d11838
|
@ -60,7 +60,7 @@ class RelatedFilterSpec(FilterSpec):
|
||||||
self.lookup_title = f.verbose_name
|
self.lookup_title = f.verbose_name
|
||||||
self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.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_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):
|
def has_output(self):
|
||||||
return len(self.lookup_choices) > 1
|
return len(self.lookup_choices) > 1
|
||||||
|
@ -72,8 +72,7 @@ class RelatedFilterSpec(FilterSpec):
|
||||||
yield {'selected': self.lookup_val is None,
|
yield {'selected': self.lookup_val is None,
|
||||||
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
|
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
|
||||||
'display': _('All')}
|
'display': _('All')}
|
||||||
for val in self.lookup_choices:
|
for pk_val, val in self.lookup_choices:
|
||||||
pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
|
|
||||||
yield {'selected': self.lookup_val == smart_unicode(pk_val),
|
yield {'selected': self.lookup_val == smart_unicode(pk_val),
|
||||||
'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
|
'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
|
||||||
'display': val}
|
'display': val}
|
||||||
|
|
|
@ -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>
|
|
@ -74,7 +74,24 @@ class ModelWithStringPrimaryKey(models.Model):
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.id
|
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(Article, ArticleAdmin)
|
||||||
admin.site.register(CustomArticle, CustomArticleAdmin)
|
admin.site.register(CustomArticle, CustomArticleAdmin)
|
||||||
admin.site.register(Section, inlines=[ArticleInline])
|
admin.site.register(Section, inlines=[ArticleInline])
|
||||||
admin.site.register(ModelWithStringPrimaryKey)
|
admin.site.register(ModelWithStringPrimaryKey)
|
||||||
|
admin.site.register(Color)
|
||||||
|
admin.site.register(Thing, ThingAdmin)
|
||||||
|
|
|
@ -12,7 +12,7 @@ from django.utils.html import escape
|
||||||
from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
|
from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
|
||||||
|
|
||||||
class AdminViewBasicTest(TestCase):
|
class AdminViewBasicTest(TestCase):
|
||||||
fixtures = ['admin-views-users.xml']
|
fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client.login(username='super', password='secret')
|
self.client.login(username='super', password='secret')
|
||||||
|
@ -148,6 +148,19 @@ class AdminViewBasicTest(TestCase):
|
||||||
"Results of sorting on ModelAdmin method are out of order."
|
"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):
|
def get_perm(Model, perm):
|
||||||
"""Return the permission object, for the Model"""
|
"""Return the permission object, for the Model"""
|
||||||
ct = ContentType.objects.get_for_model(Model)
|
ct = ContentType.objects.get_for_model(Model)
|
||||||
|
|
Loading…
Reference in New Issue