From 314e9cd38f6bf72a619e5d07c99371439c33ba11 Mon Sep 17 00:00:00 2001 From: Emmanuelle Delescolle Date: Sun, 5 Oct 2014 20:06:51 +0200 Subject: [PATCH] [1.5.x] Fixed #23604 -- Allowed related m2m fields to be references in the admin. Thanks Simon Charette for review. Backport of a24cf21722 from master --- django/contrib/admin/options.py | 5 +++++ docs/releases/1.4.16.txt | 9 ++++++++- docs/releases/1.5.11.txt | 5 ++++- tests/regressiontests/admin_views/admin.py | 3 ++- tests/regressiontests/admin_views/models.py | 9 +++++++++ tests/regressiontests/admin_views/tests.py | 4 ++++ 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index fa378148e7..bf88e9e014 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -287,6 +287,11 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)): except FieldDoesNotExist: return False + # Check whether this model is the origin of a M2M relationship + # in which case to_field has to be the pk on this model. + if opts.many_to_many and field.primary_key: + return True + # Make sure at least one of the models registered for this site # references this field through a FK or a M2M relationship. registered_models = set() diff --git a/docs/releases/1.4.16.txt b/docs/releases/1.4.16.txt index 7c6e2675a0..23e9ecd380 100644 --- a/docs/releases/1.4.16.txt +++ b/docs/releases/1.4.16.txt @@ -4,10 +4,17 @@ Django 1.4.16 release notes *Under development* -Django 1.4.16 fixes a regression in the 1.4.14 security release. +Django 1.4.16 fixes a couple regressions in the 1.4.14 security release and a +bug preventing the use of some GEOS versions with GeoDjango. Bugfixes ======== +* Allowed related many-to-many fields to be referenced in the admin + (`#23604 `_). + * Allowed inline and hidden references to admin fields (`#23431 `_). + +* Fixed parsing of the GEOS version string + (`#20036 `_). diff --git a/docs/releases/1.5.11.txt b/docs/releases/1.5.11.txt index 9a60239c64..3c6f4c42ac 100644 --- a/docs/releases/1.5.11.txt +++ b/docs/releases/1.5.11.txt @@ -4,10 +4,13 @@ Django 1.5.11 release notes *Under development* -Django 1.5.11 fixes a regression in the 1.5.9 security release. +Django 1.5.11 fixes a couple regressions in the 1.5.9 security release. Bugfixes ======== +* Allowed related many-to-many fields to be referenced in the admin + (`#23604 `_). + * Allowed inline and hidden references to admin fields (`#23431 `_). diff --git a/tests/regressiontests/admin_views/admin.py b/tests/regressiontests/admin_views/admin.py index d3c199d8c4..6d376027d2 100644 --- a/tests/regressiontests/admin_views/admin.py +++ b/tests/regressiontests/admin_views/admin.py @@ -29,7 +29,7 @@ from .models import (Article, Chapter, Account, Media, Child, Parent, Picture, AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated, RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice, ShortMessage, Telegram, ReferencedByParent, ChildOfReferer, M2MReference, - ReferencedByInline, InlineReference, InlineReferer) + ReferencedByInline, InlineReference, InlineReferer, Ingredient) def callable_year(dt_value): @@ -746,6 +746,7 @@ site.register(Color2, CustomTemplateFilterColorAdmin) site.register(Simple, AttributeErrorRaisingAdmin) site.register(UserMessenger, MessageTestingAdmin) site.register(Choice, ChoiceList) +site.register(Ingredient) # Register core models we need in our tests from django.contrib.auth.models import User, Group diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index b3130a304f..9d99ec5d77 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -708,3 +708,12 @@ class InlineReference(models.Model): class InlineReferer(models.Model): refs = models.ManyToManyField(InlineReference) + +# Models for #23604 +class Recipe(models.Model): + name = models.CharField(max_length=20) + + +class Ingredient(models.Model): + name = models.CharField(max_length=20) + recipes = models.ManyToManyField('Recipe', related_name='ingredients') diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index a85f115792..a8af47a11d 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -575,6 +575,10 @@ class AdminViewBasicTest(TestCase): response = self.client.get("/test_admin/admin/admin_views/m2mreference/", {TO_FIELD_VAR: 'id'}) self.assertEqual(response.status_code, 200) + # #23604 - Specifying the pk of this model should be allowed when this model defines a m2m relationship + response = self.client.get("/test_admin/admin/admin_views/ingredient/", {TO_FIELD_VAR: 'id'}) + self.assertEqual(response.status_code, 200) + # #23329 - 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'})