Author: kmtracey
Date: 2010-03-08 18:38:53 -0600 (Mon, 08 Mar 2010)
New Revision: 12734

Modified:
   django/trunk/django/contrib/admin/validation.py
   django/trunk/tests/regressiontests/admin_validation/models.py
Log:
Fixed #12689: Fixed admin validation to report an error on invalid exclude 
specification. Thanks for report to bparker and for patch with tests to ramiro.


Modified: django/trunk/django/contrib/admin/validation.py
===================================================================
--- django/trunk/django/contrib/admin/validation.py     2010-03-08 23:55:04 UTC 
(rev 12733)
+++ django/trunk/django/contrib/admin/validation.py     2010-03-09 00:38:53 UTC 
(rev 12734)
@@ -267,6 +267,19 @@
         if len(flattened_fieldsets) > len(set(flattened_fieldsets)):
             raise ImproperlyConfigured('There are duplicate field(s) in 
%s.fieldsets' % cls.__name__)
 
+    # exclude
+    if cls.exclude: # default value is None
+        check_isseq(cls, 'exclude', cls.exclude)
+        for field in cls.exclude:
+            check_formfield(cls, model, opts, 'exclude', field)
+            try:
+                f = opts.get_field(field)
+            except models.FieldDoesNotExist:
+                # If we can't find a field on the model that matches,
+                # it could be an extra field on the form.
+                continue
+        if len(cls.exclude) > len(set(cls.exclude)):
+            raise ImproperlyConfigured('There are duplicate field(s) in 
%s.exclude' % cls.__name__)
 
     # form
     if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm):

Modified: django/trunk/tests/regressiontests/admin_validation/models.py
===================================================================
--- django/trunk/tests/regressiontests/admin_validation/models.py       
2010-03-08 23:55:04 UTC (rev 12733)
+++ django/trunk/tests/regressiontests/admin_validation/models.py       
2010-03-09 00:38:53 UTC (rev 12734)
@@ -72,6 +72,37 @@
     ...
 ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is 
missing from the form.
 
+# Tests for basic validation of 'exclude' option values (#12689)
+
+>>> class ExcludedFields1(admin.ModelAdmin):
+...     exclude = ('foo')
+
+>>> validate(ExcludedFields1, Book)
+Traceback (most recent call last):
+    ...
+ImproperlyConfigured: 'ExcludedFields1.exclude' must be a list or tuple.
+
+>>> class ExcludedFields2(admin.ModelAdmin):
+...     exclude = ('name', 'name')
+
+>>> validate(ExcludedFields2, Book)
+Traceback (most recent call last):
+    ...
+ImproperlyConfigured: There are duplicate field(s) in ExcludedFields2.exclude
+
+>>> class ExcludedFieldsInline(admin.TabularInline):
+...     model = Song
+...     exclude = ('foo')
+
+>>> class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
+...     model = Album
+...     inlines = [ExcludedFieldsInline]
+
+>>> validate(ExcludedFieldsAlbumAdmin, Album)
+Traceback (most recent call last):
+    ...
+ImproperlyConfigured: 'ExcludedFieldsInline.exclude' must be a list or tuple.
+
 # Regression test for #9932 - exclude in InlineModelAdmin
 # should not contain the ForeignKey field used in ModelAdmin.model
 

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

Reply via email to