Fixed #7918 -- Allow the foreign key in an inline to be any where in the parent chain. Thanks sil for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8708 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-08-29 17:41:40 +00:00
parent 3717e3bba3
commit d57d490b76
2 changed files with 34 additions and 2 deletions

View File

@ -385,7 +385,7 @@ def _get_foreign_key(parent_model, model, fk_name=None):
fk = fks_to_parent[0] fk = fks_to_parent[0]
if not isinstance(fk, ForeignKey) or \ if not isinstance(fk, ForeignKey) or \
(fk.rel.to != parent_model and (fk.rel.to != parent_model and
fk.rel.to not in parent_model._meta.parents.keys()): fk.rel.to not in parent_model._meta.get_parent_list()):
raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model))
elif len(fks_to_parent) == 0: elif len(fks_to_parent) == 0:
raise Exception("%s has no field named '%s'" % (model, fk_name)) raise Exception("%s has no field named '%s'" % (model, fk_name))
@ -395,7 +395,7 @@ def _get_foreign_key(parent_model, model, fk_name=None):
f for f in opts.fields f for f in opts.fields
if isinstance(f, ForeignKey) if isinstance(f, ForeignKey)
and (f.rel.to == parent_model and (f.rel.to == parent_model
or f.rel.to in parent_model._meta.parents.keys()) or f.rel.to in parent_model._meta.get_parent_list())
] ]
if len(fks_to_parent) == 1: if len(fks_to_parent) == 1:
fk = fks_to_parent[0] fk = fks_to_parent[0]

View File

@ -37,6 +37,29 @@ class CustomPrimaryKey(models.Model):
some_field = models.CharField(max_length=100) some_field = models.CharField(max_length=100)
# models for inheritance tests.
class Place(models.Model):
name = models.CharField(max_length=50)
city = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Owner(models.Model):
name = models.CharField(max_length=100)
place = models.ForeignKey(Place)
class Restaurant(Place):
serves_pizza = models.BooleanField()
def __unicode__(self):
return self.name
class MexicanRestaurant(Restaurant):
serves_tacos = models.BooleanField()
__test__ = {'API_TESTS': """ __test__ = {'API_TESTS': """
>>> from datetime import date >>> from datetime import date
@ -396,4 +419,13 @@ We need to ensure that it is displayed
<p><label for="id_form-0-my_pk">My pk:</label> <input id="id_form-0-my_pk" type="text" name="form-0-my_pk" maxlength="10" /></p> <p><label for="id_form-0-my_pk">My pk:</label> <input id="id_form-0-my_pk" type="text" name="form-0-my_pk" maxlength="10" /></p>
<p><label for="id_form-0-some_field">Some field:</label> <input id="id_form-0-some_field" type="text" name="form-0-some_field" maxlength="100" /></p> <p><label for="id_form-0-some_field">Some field:</label> <input id="id_form-0-some_field" type="text" name="form-0-some_field" maxlength="100" /></p>
# Foreign keys in parents ########################################
>>> from django.forms.models import _get_foreign_key
>>> type(_get_foreign_key(Restaurant, Owner))
<class 'django.db.models.fields.related.ForeignKey'>
>>> type(_get_foreign_key(MexicanRestaurant, Owner))
<class 'django.db.models.fields.related.ForeignKey'>
"""} """}