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] Fixed #12689: Fixed admin validation to report an error on in…

…valid exclude specification. Thanks for report to bparker and for patch with tests to ramiro.

r12734 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12735 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f13b17ad1574bd6162ef058079c534d8e5aa2b22 1 parent 0cac2cb
Karen Tracey authored March 09, 2010
14  django/contrib/admin/validation.py
@@ -220,6 +220,20 @@ def validate_base(cls, model):
220 220
         for field in flattened_fieldsets:
221 221
             check_formfield(cls, model, opts, "fieldsets[%d][1]['fields']" % idx, field)
222 222
 
  223
+    # exclude
  224
+    if cls.exclude: # default value is None
  225
+        check_isseq(cls, 'exclude', cls.exclude)
  226
+        for field in cls.exclude:
  227
+            check_formfield(cls, model, opts, 'exclude', field)
  228
+            try:
  229
+                f = opts.get_field(field)
  230
+            except models.FieldDoesNotExist:
  231
+                # If we can't find a field on the model that matches,
  232
+                # it could be an extra field on the form.
  233
+                continue
  234
+        if len(cls.exclude) > len(set(cls.exclude)):
  235
+            raise ImproperlyConfigured('There are duplicate field(s) in %s.exclude' % cls.__name__)
  236
+
223 237
     # form
224 238
     if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm):
225 239
         raise ImproperlyConfigured("%s.form does not inherit from "
34  tests/regressiontests/admin_validation/models.py
@@ -25,7 +25,8 @@ class TwoAlbumFKAndAnE(models.Model):
25 25
     album2 = models.ForeignKey(Album, related_name="album2_set")
26 26
     e = models.CharField(max_length=1)
27 27
 
28  
-
  28
+class Book(models.Model):
  29
+    name = models.CharField(max_length=100)
29 30
 
30 31
 __test__ = {'API_TESTS':"""
31 32
 
@@ -52,6 +53,37 @@ class TwoAlbumFKAndAnE(models.Model):
52 53
     ...
53 54
 ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is missing from the form.
54 55
 
  56
+# Tests for basic validation of 'exclude' option values (#12689)
  57
+
  58
+>>> class ExcludedFields1(admin.ModelAdmin):
  59
+...     exclude = ('foo')
  60
+
  61
+>>> validate(ExcludedFields1, Book)
  62
+Traceback (most recent call last):
  63
+    ...
  64
+ImproperlyConfigured: 'ExcludedFields1.exclude' must be a list or tuple.
  65
+
  66
+>>> class ExcludedFields2(admin.ModelAdmin):
  67
+...     exclude = ('name', 'name')
  68
+
  69
+>>> validate(ExcludedFields2, Book)
  70
+Traceback (most recent call last):
  71
+    ...
  72
+ImproperlyConfigured: There are duplicate field(s) in ExcludedFields2.exclude
  73
+
  74
+>>> class ExcludedFieldsInline(admin.TabularInline):
  75
+...     model = Song
  76
+...     exclude = ('foo')
  77
+
  78
+>>> class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
  79
+...     model = Album
  80
+...     inlines = [ExcludedFieldsInline]
  81
+
  82
+>>> validate(ExcludedFieldsAlbumAdmin, Album)
  83
+Traceback (most recent call last):
  84
+    ...
  85
+ImproperlyConfigured: 'ExcludedFieldsInline.exclude' must be a list or tuple.
  86
+
55 87
 # Regression test for #9932 - exclude in InlineModelAdmin
56 88
 # should not contain the ForeignKey field used in ModelAdmin.model
57 89
 

0 notes on commit f13b17a

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