Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Moved the call to _get_foreign_key to run in all cases catchi…

…ng incorrect inline setup sooner.

Backport of [11631] from trunk

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11633 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bde4c6d9955b2df78aed2f7ac540b017e4de3584 1 parent fb8653a
Brian Rosner authored October 19, 2009
4  django/contrib/admin/validation.py
@@ -149,6 +149,9 @@ def validate(cls, model):
149 149
             validate_inline(inline, cls, model)
150 150
 
151 151
 def validate_inline(cls, parent, parent_model):
  152
+    
  153
+    fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True)
  154
+    
152 155
     # model is already verified to exist and be a Model
153 156
     if cls.fk_name: # default value is None
154 157
         f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
@@ -169,7 +172,6 @@ def validate_inline(cls, parent, parent_model):
169 172
 
170 173
     # exclude
171 174
     if hasattr(cls, 'exclude') and cls.exclude:
172  
-        fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True)
173 175
         if fk and fk.name in cls.exclude:
174 176
             raise ImproperlyConfigured("%s cannot exclude the field "
175 177
                     "'%s' - this is the foreign key to the parent model "
24  tests/regressiontests/admin_validation/models.py
@@ -20,7 +20,7 @@ def __unicode__(self):
20 20
         return self.title
21 21
 
22 22
 
23  
-class Model11709(models.Model):
  23
+class TwoAlbumFKAndAnE(models.Model):
24 24
     album1 = models.ForeignKey(Album, related_name="album1_set")
25 25
     album2 = models.ForeignKey(Album, related_name="album2_set")
26 26
     e = models.CharField(max_length=1)
@@ -72,11 +72,27 @@ class Model11709(models.Model):
72 72
 # given) make sure fk_name is honored or things blow up when there is more
73 73
 # than one fk to the parent model.
74 74
 
75  
->>> class Model11709Inline(admin.TabularInline):
76  
-...     model = Model11709
  75
+>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
  76
+...     model = TwoAlbumFKAndAnE
77 77
 ...     exclude = ("e",)
78 78
 ...     fk_name = "album1"
79 79
 
80  
->>> validate_inline(Model11709Inline, None, Album)
  80
+>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
  81
+
  82
+# Ensure inlines validate that they can be used correctly.
  83
+
  84
+>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
  85
+...     model = TwoAlbumFKAndAnE
  86
+
  87
+>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
  88
+Traceback (most recent call last):
  89
+    ...
  90
+Exception: <class 'regressiontests.admin_validation.models.TwoAlbumFKAndAnE'> has more than 1 ForeignKey to <class 'regressiontests.admin_validation.models.Album'>
  91
+
  92
+>>> class TwoAlbumFKAndAnEInline(admin.TabularInline):
  93
+...     model = TwoAlbumFKAndAnE
  94
+...     fk_name = "album1"
  95
+
  96
+>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
81 97
 
82 98
 """}

0 notes on commit bde4c6d

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