Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.4.x] Fixed #23329 -- Allowed inherited and m2m fields to be refere…

…nced in the admin.

Thanks to Trac alias Markush2010 and ross for the detailed reports.

Backport of 3cbb759 from master
  • Loading branch information...
commit 4685026840f0e2b895f980b6a33ad1b282aa7852 1 parent 8adc56c
@charettes charettes authored
View
10 django/contrib/admin/options.py
@@ -278,11 +278,13 @@ def to_field_allowed(self, request, to_field):
return False
# Make sure at least one of the models registered for this site
- # references this field.
+ # references this field through a FK or a M2M relationship.
registered_models = self.admin_site._registry
- for related_object in opts.get_all_related_objects():
- if (related_object.model in registered_models and
- field == related_object.field.rel.get_related_field()):
+ for related_object in (opts.get_all_related_objects() +
+ opts.get_all_related_many_to_many_objects()):
+ related_model = related_object.model
+ if (any(issubclass(model, related_model) for model in registered_models) and
+ related_object.field.rel.get_related_field() == field):
return True
return False
View
13 docs/releases/1.4.15.txt
@@ -0,0 +1,13 @@
+===========================
+Django 1.4.15 release notes
+===========================
+
+*Under development*
+
+Django 1.4.15 fixes a regression in the 1.4.14 security release.
+
+Bugfixes
+========
+
+* Allowed inherited and m2m fields to be referenced in the admin
+ (`#22486 <http://code.djangoproject.com/ticket/23329>`_)
View
1  docs/releases/index.txt
@@ -19,6 +19,7 @@ Final releases
.. toctree::
:maxdepth: 1
+ 1.4.15
1.4.14
1.4.13
1.4.12
View
5 tests/regressiontests/admin_views/admin.py
@@ -27,7 +27,7 @@
Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
- RelatedPrepopulated)
+ RelatedPrepopulated, ReferencedByParent, ChildOfReferer, M2MReference)
def callable_year(dt_value):
@@ -616,6 +616,9 @@ class UnorderedObjectAdmin(admin.ModelAdmin):
site.register(Report, ReportAdmin)
site.register(MainPrepopulated, MainPrepopulatedAdmin)
site.register(UnorderedObject, UnorderedObjectAdmin)
+site.register(ReferencedByParent)
+site.register(ChildOfReferer)
+site.register(M2MReference)
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
# That way we cover all four cases:
View
18 tests/regressiontests/admin_views/models.py
@@ -607,3 +607,21 @@ class UnorderedObject(models.Model):
"""
name = models.CharField(max_length=255)
bool = models.BooleanField(default=True)
+
+
+# Models for #23329
+class ReferencedByParent(models.Model):
+ pass
+
+
+class ParentWithFK(models.Model):
+ fk = models.ForeignKey(ReferencedByParent)
+
+
+class ChildOfReferer(ParentWithFK):
+ pass
+
+
+class M2MReference(models.Model):
+ ref = models.ManyToManyField('self')
+
View
9 tests/regressiontests/admin_views/tests.py
@@ -586,6 +586,15 @@ def test_disallowed_to_field(self):
response = self.client.get("/test_admin/admin/admin_views/section/", {TO_FIELD_VAR: 'id'})
self.assertEqual(response.status_code, 200)
+ # Specifying a field referenced by another model though a m2m should be allowed.
+ response = self.client.get("/test_admin/admin/admin_views/m2mreference/", {TO_FIELD_VAR: 'id'})
+ self.assertEqual(response.status_code, 200)
+
+ # Specifying a field that is not refered by any other model directly registered
+ # to this admin site but registered through inheritance should be allowed.
+ response = self.client.get("/test_admin/admin/admin_views/referencedbyparent/", {TO_FIELD_VAR: 'id'})
+ self.assertEqual(response.status_code, 200)
+
def test_allowed_filtering_15103(self):
"""
Regressions test for ticket 15103 - filtering on fields defined in a
Please sign in to comment.
Something went wrong with that request. Please try again.