diff options
| author | Ramiro Morales <cramm0@gmail.com> | 2011-02-16 23:24:19 +0000 |
|---|---|---|
| committer | Ramiro Morales <cramm0@gmail.com> | 2011-02-16 23:24:19 +0000 |
| commit | 12fd6e1106039269d7fe501d61951f1a1aac9fa3 (patch) | |
| tree | 40e443c45455bc2b9b2555b0e326532a2044c3b7 | |
| parent | 840314bde48f8df47b9b6f6a32b1051b9980df30 (diff) | |
[1.1.X] Fixed #15306 -- Replaced 1.1.X implementation of admin changelist filtering security fix (r15031/r15033) with the one from trunk so another valid filter usage scenario (using model inheritance) is still possible. Thanks dbenamy for reporting this. Refs #15032.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@15555 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/contrib/admin/options.py | 17 | ||||
| -rw-r--r-- | tests/regressiontests/admin_views/models.py | 12 | ||||
| -rw-r--r-- | tests/regressiontests/admin_views/tests.py | 12 |
3 files changed, 38 insertions, 3 deletions
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 71f08df87c..1419fe3772 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -192,8 +192,21 @@ class BaseModelAdmin(object): # Special case -- foo__id__exact and foo__id queries are implied # if foo has been specificially included in the lookup list; so - # drop __id if it is the last part. - if len(parts) > 1 and parts[-1] == self.model._meta.pk.name: + # drop __id if it is the last part. However, first we need to find + # the pk attribute name. + model = self.model + pk_attr_name = None + for part in parts[:-1]: + field, _, _, _ = model._meta.get_field_by_name(part) + if hasattr(field, 'rel'): + model = field.rel.to + pk_attr_name = model._meta.pk.name + elif isinstance(field, RelatedObject): + model = field.model + pk_attr_name = model._meta.pk.name + else: + pk_attr_name = None + if pk_attr_name and len(parts) > 1 and parts[-1] == pk_attr_name: parts.pop() try: diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index bce39d8846..cb7c6da030 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -533,6 +533,17 @@ class Album(models.Model): class AlbumAdmin(admin.ModelAdmin): list_filter = ['title'] +class Employee(Person): + code = models.CharField(max_length=20) + +class WorkHour(models.Model): + datum = models.DateField() + employee = models.ForeignKey(Employee) + +class WorkHourAdmin(admin.ModelAdmin): + list_display = ('datum', 'employee') + list_filter = ('employee',) + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, save_as=True, inlines=[ArticleInline]) @@ -565,6 +576,7 @@ admin.site.register(Plot) admin.site.register(PlotDetails) admin.site.register(CyclicOne) admin.site.register(CyclicTwo) +admin.site.register(WorkHour, WorkHourAdmin) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index f699acf85b..f0b591d23c 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -23,7 +23,7 @@ from models import Article, BarAccount, CustomArticle, EmptyModel, \ FooAccount, Gallery, ModelWithStringPrimaryKey, \ Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \ Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \ - Category, Plot, FunkyTag + Category, Plot, FunkyTag, WorkHour, Employee try: set @@ -311,6 +311,16 @@ class AdminViewBasicTest(TestCase): except SuspiciousOperation: self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model") + e1 = Employee.objects.create(name='Anonymous', gender=1, age=22, alive=True, code='123') + e2 = Employee.objects.create(name='Visitor', gender=2, age=19, alive=True, code='124') + WorkHour.objects.create(datum=datetime.datetime.now(), employee=e1) + WorkHour.objects.create(datum=datetime.datetime.now(), employee=e2) + response = self.client.get("/test_admin/admin/admin_views/workhour/") + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'employee__person_ptr__exact') + response = self.client.get("/test_admin/admin/admin_views/workhour/?employee__person_ptr__exact=%d" % e1.pk) + self.assertEqual(response.status_code, 200) + class SaveAsTests(TestCase): fixtures = ['admin-views-users.xml','admin-views-person.xml'] |
