Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
View
4 django/forms/models.py
@@ -385,7 +385,7 @@ def _get_foreign_key(parent_model, model, fk_name=None):
fk = fks_to_parent[0]
if not isinstance(fk, ForeignKey) or \
(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))
elif len(fks_to_parent) == 0:
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
if isinstance(f, ForeignKey)
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:
fk = fks_to_parent[0]
View
32 tests/modeltests/model_formsets/models.py
@@ -37,6 +37,29 @@ class CustomPrimaryKey(models.Model):
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': """
>>> from datetime import date
@@ -396,4 +419,13 @@ class CustomPrimaryKey(models.Model):
<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>
+# 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'>
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.