Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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
@kmtracey kmtracey authored
View
14 django/contrib/admin/validation.py
@@ -220,6 +220,20 @@ def validate_base(cls, model):
for field in flattened_fieldsets:
check_formfield(cls, model, opts, "fieldsets[%d][1]['fields']" % idx, field)
+ # 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):
raise ImproperlyConfigured("%s.form does not inherit from "
View
34 tests/regressiontests/admin_validation/models.py
@@ -25,7 +25,8 @@ class TwoAlbumFKAndAnE(models.Model):
album2 = models.ForeignKey(Album, related_name="album2_set")
e = models.CharField(max_length=1)
-
+class Book(models.Model):
+ name = models.CharField(max_length=100)
__test__ = {'API_TESTS':"""
@@ -52,6 +53,37 @@ class TwoAlbumFKAndAnE(models.Model):
...
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
Please sign in to comment.
Something went wrong with that request. Please try again.