summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2014-11-16 16:42:09 +0100
committerTim Graham <timograham@gmail.com>2014-11-25 14:00:17 -0500
commit3d35ea43001991d94c192abca1832628cd255bb0 (patch)
tree92cc8d3fb8b374cad5e46bd83cb25667891ce2a8 /tests
parent17ffd24d9b3528c5dcb0d50e86df2b03a9b288fc (diff)
[1.5.x] Fixed #23754 -- Always allowed reference to the primary key in the admin
This change allows dynamically created inlines "Add related" button to work correcly as long as their associated foreign key is pointing to the primary key of the related model. Thanks to amorce for the report, Julien Phalip for the initial patch, and Collin Anderson for the review. Backport of f9c4e14aeca7df79991bca8ac2d743953cbd095c from master
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/admin_views/admin.py5
-rw-r--r--tests/regressiontests/admin_views/models.py24
-rw-r--r--tests/regressiontests/admin_views/tests.py16
3 files changed, 30 insertions, 15 deletions
diff --git a/tests/regressiontests/admin_views/admin.py b/tests/regressiontests/admin_views/admin.py
index 6d376027d2..3299f14dd0 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, Ingredient)
+ ReferencedByInline, InlineReference, InlineReferer, Recipe, Ingredient, NotReferenced)
def callable_year(dt_value):
@@ -714,7 +714,6 @@ site.register(UnorderedObject, UnorderedObjectAdmin)
site.register(UndeletableObject, UndeletableObjectAdmin)
site.register(ReferencedByParent)
site.register(ChildOfReferer)
-site.register(M2MReference)
site.register(ReferencedByInline)
site.register(InlineReferer, InlineRefererAdmin)
@@ -746,7 +745,9 @@ site.register(Color2, CustomTemplateFilterColorAdmin)
site.register(Simple, AttributeErrorRaisingAdmin)
site.register(UserMessenger, MessageTestingAdmin)
site.register(Choice, ChoiceList)
+site.register(Recipe)
site.register(Ingredient)
+site.register(NotReferenced)
# 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 9d99ec5d77..1279db0795 100644
--- a/tests/regressiontests/admin_views/models.py
+++ b/tests/regressiontests/admin_views/models.py
@@ -682,11 +682,13 @@ class Choice(models.Model):
# Models for #23329
class ReferencedByParent(models.Model):
- pass
+ name = models.CharField(max_length=20, unique=True)
class ParentWithFK(models.Model):
- fk = models.ForeignKey(ReferencedByParent)
+ fk = models.ForeignKey(
+ ReferencedByParent, to_field='name', related_name='hidden+',
+ )
class ChildOfReferer(ParentWithFK):
@@ -696,13 +698,16 @@ class ChildOfReferer(ParentWithFK):
class M2MReference(models.Model):
ref = models.ManyToManyField('self')
+
# Models for #23431
class ReferencedByInline(models.Model):
- pass
+ name = models.CharField(max_length=20, unique=True)
class InlineReference(models.Model):
- fk = models.ForeignKey(ReferencedByInline, related_name='hidden+')
+ fk = models.ForeignKey(
+ ReferencedByInline, to_field='name', related_name='hidden+',
+ )
class InlineReferer(models.Model):
@@ -711,9 +716,14 @@ class InlineReferer(models.Model):
# Models for #23604
class Recipe(models.Model):
- name = models.CharField(max_length=20)
+ pass
class Ingredient(models.Model):
- name = models.CharField(max_length=20)
- recipes = models.ManyToManyField('Recipe', related_name='ingredients')
+ recipes = models.ManyToManyField(Recipe)
+
+
+# Model for #23839
+class NotReferenced(models.Model):
+ # Don't point any FK at this model.
+ pass
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
index a8af47a11d..bc1ffc33d3 100644
--- a/tests/regressiontests/admin_views/tests.py
+++ b/tests/regressiontests/admin_views/tests.py
@@ -567,26 +567,30 @@ class AdminViewBasicTest(TestCase):
with self.assertRaises(DisallowedModelAdminToField):
response = self.client.get("/test_admin/admin/admin_views/section/", {TO_FIELD_VAR: 'name'})
- # Specifying a field referenced by another model should be allowed.
- response = self.client.get("/test_admin/admin/admin_views/section/", {TO_FIELD_VAR: 'id'})
+ # #23839 - Primary key should always be allowed, even if the referenced model isn't registered.
+ response = self.client.get("/test_admin/admin/admin_views/notreferenced/", {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'})
+ # XXX: We're not testing against a non-primary key field since the admin doesn't
+ # support it yet, ref #23862
+ response = self.client.get("/test_admin/admin/admin_views/recipe/", {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
+ # #23604 - Specifying a field referenced through a reverse m2m relationship should be allowed.
+ # XXX: We're not testing against a non-primary key field since the admin doesn't
+ # support it yet, ref #23862
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'})
+ response = self.client.get("/test_admin/admin/admin_views/referencedbyparent/", {TO_FIELD_VAR: 'name'})
self.assertEqual(response.status_code, 200)
# #23431 - Specifying a field that is only refered to by a inline of a registered
# model should be allowed.
- response = self.client.get("/test_admin/admin/admin_views/referencedbyinline/", {TO_FIELD_VAR: 'id'})
+ response = self.client.get("/test_admin/admin/admin_views/referencedbyinline/", {TO_FIELD_VAR: 'name'})
self.assertEqual(response.status_code, 200)
def test_allowed_filtering_15103(self):