diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2013-11-21 14:11:57 +0200 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-11-21 15:34:04 +0200 |
| commit | 5730bf281922151f211a2509dd8b493230d6a187 (patch) | |
| tree | 7e2dc1ec9a1fde8a1d93bc2bcf212ad0449d44dd | |
| parent | 47abfa237e21ca71d8ee901cf8ba1f1c89ae1f2e (diff) | |
[1.6.x] Fixed #21431 -- GenRel->FK list_filter regression in admin
Report, analysis and tests from stephenmcd.
Backpatch of 752d3d70da6291039f33781a0a2ef6f3b7c5fcb5 from master.
| -rw-r--r-- | django/contrib/admin/util.py | 4 | ||||
| -rw-r--r-- | tests/admin_views/tests.py | 19 |
2 files changed, 22 insertions, 1 deletions
diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index 962d8f7f1d..177b2dacc5 100644 --- a/django/contrib/admin/util.py +++ b/django/contrib/admin/util.py @@ -379,7 +379,9 @@ class NotRelationField(Exception): def get_model_from_relation(field): - if isinstance(field, models.related.RelatedObject): + if hasattr(field, 'get_path_info'): + return field.get_path_info()[-1].to_opts.model + elif isinstance(field, models.related.RelatedObject): return field.model elif getattr(field, 'rel'): # or isinstance? return field.rel.to diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 2ec5c87949..8a53dee3ea 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -7,6 +7,7 @@ import datetime from django.conf import settings, global_settings from django.core import mail +from django.core.exceptions import ImproperlyConfigured from django.core.files import temp as tempfile from django.core.urlresolvers import reverse # Register auth models with the admin. @@ -16,6 +17,7 @@ from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.models import LogEntry, DELETION from django.contrib.admin.sites import LOGIN_FORM_KEY from django.contrib.admin.util import quote +from django.contrib.admin.validation import ModelAdminValidator from django.contrib.admin.views.main import IS_POPUP_VAR from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.auth import REDIRECT_FIELD_NAME @@ -4361,3 +4363,20 @@ class AdminKeepChangeListFiltersTests(TestCase): class NamespacedAdminKeepChangeListFiltersTests(AdminKeepChangeListFiltersTests): admin_site = site2 + + +class AdminGenericRelationTests(TestCase): + def test_generic_relation_fk_list_filter(self): + """ + Validates a model with a generic relation to a model with + a foreign key can specify the generic+fk relationship + path as a list_filter. See trac #21428. + """ + class GenericFKAdmin(admin.ModelAdmin): + list_filter = ('tags__content_type',) + + validator = ModelAdminValidator() + try: + validator.validate_list_filter(GenericFKAdmin, Plot) + except ImproperlyConfigured: + self.fail("Couldn't validate a GenericRelation -> FK path in ModelAdmin.list_filter") |
