Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit d57d490b76943afabf6df0159817d7a5756c0742 1 parent 3717e3b
Brian Rosner authored August 29, 2008
4  django/forms/models.py
@@ -385,7 +385,7 @@ def _get_foreign_key(parent_model, model, fk_name=None):
385 385
             fk = fks_to_parent[0]
386 386
             if not isinstance(fk, ForeignKey) or \
387 387
                     (fk.rel.to != parent_model and
388  
-                     fk.rel.to not in parent_model._meta.parents.keys()):
  388
+                     fk.rel.to not in parent_model._meta.get_parent_list()):
389 389
                 raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model))
390 390
         elif len(fks_to_parent) == 0:
391 391
             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):
395 395
             f for f in opts.fields
396 396
             if isinstance(f, ForeignKey)
397 397
             and (f.rel.to == parent_model
398  
-                or f.rel.to in parent_model._meta.parents.keys())
  398
+                or f.rel.to in parent_model._meta.get_parent_list())
399 399
         ]
400 400
         if len(fks_to_parent) == 1:
401 401
             fk = fks_to_parent[0]
32  tests/modeltests/model_formsets/models.py
@@ -37,6 +37,29 @@ class CustomPrimaryKey(models.Model):
37 37
     some_field = models.CharField(max_length=100)
38 38
 
39 39
 
  40
+# models for inheritance tests.
  41
+
  42
+class Place(models.Model):
  43
+    name = models.CharField(max_length=50)
  44
+    city = models.CharField(max_length=50)
  45
+    
  46
+    def __unicode__(self):
  47
+        return self.name
  48
+
  49
+class Owner(models.Model):
  50
+    name = models.CharField(max_length=100)
  51
+    place = models.ForeignKey(Place)
  52
+
  53
+class Restaurant(Place):
  54
+    serves_pizza = models.BooleanField()
  55
+    
  56
+    def __unicode__(self):
  57
+        return self.name
  58
+
  59
+class MexicanRestaurant(Restaurant):
  60
+    serves_tacos = models.BooleanField()
  61
+
  62
+
40 63
 __test__ = {'API_TESTS': """
41 64
 
42 65
 >>> from datetime import date
@@ -396,4 +419,13 @@ class CustomPrimaryKey(models.Model):
396 419
 <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>
397 420
 <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>
398 421
 
  422
+# Foreign keys in parents ########################################
  423
+
  424
+>>> from django.forms.models import _get_foreign_key
  425
+
  426
+>>> type(_get_foreign_key(Restaurant, Owner))
  427
+<class 'django.db.models.fields.related.ForeignKey'>
  428
+>>> type(_get_foreign_key(MexicanRestaurant, Owner))
  429
+<class 'django.db.models.fields.related.ForeignKey'>
  430
+
399 431
 """}

0 notes on commit d57d490

Please sign in to comment.
Something went wrong with that request. Please try again.