Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9932 -- Added a validation error when an inline tries to exclu…

…de the foreign key that provides the link to the parent model. Thanks to david for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10668 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d7233e7c5adb8a885cb7265f58ef8a3baedbf8e7 1 parent 80a54dd
Russell Keith-Magee authored
14  django/contrib/admin/validation.py
@@ -5,7 +5,7 @@
5 5
 
6 6
 from django.core.exceptions import ImproperlyConfigured
7 7
 from django.db import models
8  
-from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model
  8
+from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model, _get_foreign_key
9 9
 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
10 10
 from django.contrib.admin.options import HORIZONTAL, VERTICAL
11 11
 
@@ -146,9 +146,9 @@ def validate(cls, model):
146 146
                 raise ImproperlyConfigured("'%s.inlines[%d].model' does not "
147 147
                         "inherit from models.Model." % (cls.__name__, idx))
148 148
             validate_base(inline, inline.model)
149  
-            validate_inline(inline)
  149
+            validate_inline(inline, cls, model)
150 150
 
151  
-def validate_inline(cls):
  151
+def validate_inline(cls, parent, parent_model):
152 152
     # model is already verified to exist and be a Model
153 153
     if cls.fk_name: # default value is None
154 154
         f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
@@ -167,6 +167,14 @@ def validate_inline(cls):
167 167
         raise ImproperlyConfigured("'%s.formset' does not inherit from "
168 168
                 "BaseModelFormSet." % cls.__name__)
169 169
 
  170
+    # exclude
  171
+    if hasattr(cls, 'exclude') and cls.exclude:
  172
+        fk_name = _get_foreign_key(parent_model, cls.model).name
  173
+        if fk_name in cls.exclude:
  174
+            raise ImproperlyConfigured("%s cannot exclude the field "
  175
+                    "'%s' - this is the foreign key to the parent model "
  176
+                    "%s." % (cls.__name__, fk_name, parent_model.__name__))
  177
+
170 178
 def validate_base(cls, model):
171 179
     opts = model._meta
172 180
 
26  tests/regressiontests/admin_validation/models.py
@@ -4,12 +4,16 @@
4 4
 
5 5
 from django.db import models
6 6
 
  7
+class Album(models.Model):
  8
+    title = models.CharField(max_length=150)
  9
+
7 10
 class Song(models.Model):
8 11
     title = models.CharField(max_length=150)
9  
-    
  12
+    album = models.ForeignKey(Album)
  13
+
10 14
     class Meta:
11 15
         ordering = ('title',)
12  
-        
  16
+
13 17
     def __unicode__(self):
14 18
         return self.title
15 19
 
@@ -19,9 +23,7 @@ def __unicode__(self):
19 23
 >>> from django.contrib import admin
20 24
 >>> from django.contrib.admin.validation import validate
21 25
 
22  
-#
23 26
 # Regression test for #8027: custom ModelForms with fields/fieldsets
24  
-#
25 27
 
26 28
 >>> class SongForm(forms.ModelForm):
27 29
 ...     pass
@@ -40,4 +42,20 @@ def __unicode__(self):
40 42
     ...
41 43
 ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is missing from the form.
42 44
 
  45
+# Regression test for #9932 - exclude in InlineModelAdmin
  46
+# should not contain the ForeignKey field used in ModelAdmin.model
  47
+
  48
+>>> class SongInline(admin.StackedInline):
  49
+...     model = Song
  50
+...     exclude = ['album']
  51
+
  52
+>>> class AlbumAdmin(admin.ModelAdmin):
  53
+...     model = Album
  54
+...     inlines = [SongInline]
  55
+
  56
+>>> validate(AlbumAdmin, Album)
  57
+Traceback (most recent call last):
  58
+    ...
  59
+ImproperlyConfigured: SongInline cannot exclude the field 'album' - this is the foreign key to the parent model Album.
  60
+
43 61
 """}

0 notes on commit d7233e7

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