Author: russellm
Date: 2009-05-03 08:48:27 -0500 (Sun, 03 May 2009)
New Revision: 10672

Modified:
   django/branches/releases/1.0.X/django/contrib/admin/validation.py
   
django/branches/releases/1.0.X/tests/regressiontests/admin_validation/models.py
Log:
[1.0.X] Fixed #9932 -- Added a validation error when an inline tries to exclude 
the foreign key that provides the link to the parent model. Thanks to david for 
the report and patch.

Merge of r10668 from trunk.

Modified: django/branches/releases/1.0.X/django/contrib/admin/validation.py
===================================================================
--- django/branches/releases/1.0.X/django/contrib/admin/validation.py   
2009-05-03 13:47:36 UTC (rev 10671)
+++ django/branches/releases/1.0.X/django/contrib/admin/validation.py   
2009-05-03 13:48:27 UTC (rev 10672)
@@ -5,7 +5,7 @@
 
 from django.core.exceptions import ImproperlyConfigured
 from django.db import models
-from django.forms.models import BaseModelForm, BaseModelFormSet, 
fields_for_model
+from django.forms.models import BaseModelForm, BaseModelFormSet, 
fields_for_model, _get_foreign_key
 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
 from django.contrib.admin.options import HORIZONTAL, VERTICAL
 
@@ -117,9 +117,9 @@
                 raise ImproperlyConfigured("'%s.inlines[%d].model' does not "
                         "inherit from models.Model." % (cls.__name__, idx))
             validate_base(inline, inline.model)
-            validate_inline(inline)
+            validate_inline(inline, cls, model)
 
-def validate_inline(cls):
+def validate_inline(cls, parent, parent_model):
     # model is already verified to exist and be a Model
     if cls.fk_name: # default value is None
         f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
@@ -138,6 +138,14 @@
         raise ImproperlyConfigured("'%s.formset' does not inherit from "
                 "BaseModelFormSet." % cls.__name__)
 
+    # exclude
+    if hasattr(cls, 'exclude') and cls.exclude:
+        fk_name = _get_foreign_key(parent_model, cls.model).name
+        if fk_name in cls.exclude:
+            raise ImproperlyConfigured("%s cannot exclude the field "
+                    "'%s' - this is the foreign key to the parent model "
+                    "%s." % (cls.__name__, fk_name, parent_model.__name__))
+
 def validate_base(cls, model):
     opts = model._meta
 

Modified: 
django/branches/releases/1.0.X/tests/regressiontests/admin_validation/models.py
===================================================================
--- 
django/branches/releases/1.0.X/tests/regressiontests/admin_validation/models.py 
    2009-05-03 13:47:36 UTC (rev 10671)
+++ 
django/branches/releases/1.0.X/tests/regressiontests/admin_validation/models.py 
    2009-05-03 13:48:27 UTC (rev 10672)
@@ -4,12 +4,16 @@
 
 from django.db import models
 
+class Album(models.Model):
+    title = models.CharField(max_length=150)
+
 class Song(models.Model):
     title = models.CharField(max_length=150)
-    
+    album = models.ForeignKey(Album)
+
     class Meta:
         ordering = ('title',)
-        
+
     def __unicode__(self):
         return self.title
 
@@ -19,9 +23,7 @@
 >>> from django.contrib import admin
 >>> from django.contrib.admin.validation import validate
 
-#
 # Regression test for #8027: custom ModelForms with fields/fieldsets
-#
 
 >>> class SongForm(forms.ModelForm):
 ...     pass
@@ -40,4 +42,20 @@
     ...
 ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is 
missing from the form.
 
+# Regression test for #9932 - exclude in InlineModelAdmin
+# should not contain the ForeignKey field used in ModelAdmin.model
+
+>>> class SongInline(admin.StackedInline):
+...     model = Song
+...     exclude = ['album']
+
+>>> class AlbumAdmin(admin.ModelAdmin):
+...     model = Album
+...     inlines = [SongInline]
+
+>>> validate(AlbumAdmin, Album)
+Traceback (most recent call last):
+    ...
+ImproperlyConfigured: SongInline cannot exclude the field 'album' - this is 
the foreign key to the parent model Album.
+
 """}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to