diff --git a/django/forms/models.py b/django/forms/models.py index cca5cae92c..bd9597d67a 100644 --- a/django/forms/models.py +++ b/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] diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index 5ce4797e20..65987c1b43 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/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 @@ We need to ensure that it is displayed
+# Foreign keys in parents ######################################## + +>>> from django.forms.models import _get_foreign_key + +>>> type(_get_foreign_key(Restaurant, Owner)) +