diff options
| author | Karen Tracey <kmtracey@gmail.com> | 2010-03-09 00:38:53 +0000 |
|---|---|---|
| committer | Karen Tracey <kmtracey@gmail.com> | 2010-03-09 00:38:53 +0000 |
| commit | bb72aca5d4783a87bd425cae20da38b6b3405639 (patch) | |
| tree | aa7f4e8451ce3a596cbb5c329d6cfd3ccf931d3c | |
| parent | 48cd8e856fa027e0e699e5a5ed7e3eb8f8450abb (diff) | |
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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12734 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/contrib/admin/validation.py | 13 | ||||
| -rw-r--r-- | tests/regressiontests/admin_validation/models.py | 31 |
2 files changed, 44 insertions, 0 deletions
diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index f743f2c6c7..6bcd52e061 100644 --- a/django/contrib/admin/validation.py +++ b/django/contrib/admin/validation.py @@ -267,6 +267,19 @@ def validate_base(cls, model): 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): diff --git a/tests/regressiontests/admin_validation/models.py b/tests/regressiontests/admin_validation/models.py index b50764844e..d927574538 100644 --- a/tests/regressiontests/admin_validation/models.py +++ b/tests/regressiontests/admin_validation/models.py @@ -72,6 +72,37 @@ Traceback (most recent call last): ... 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 |
