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.
This commit is contained in:
Simon Charette 2014-11-16 16:42:09 +01:00 committed by Tim Graham
parent e0d1f2684a
commit f9c4e14aec
9 changed files with 67 additions and 27 deletions

View file

@ -827,28 +827,28 @@ class Worker(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):
pass
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):
@ -857,9 +857,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