Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #8027: correctly validate fields/fieldsets in `ModelAdmin` vali…

…dation when using custom `ModelForm`s.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8662 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b31568aed34d8728c153ba4e722b7018f2667351 1 parent b99cc93
@jacobian jacobian authored
View
2  django/contrib/admin/validation.py
@@ -251,7 +251,7 @@ def get_field(cls, model, opts, label, field):
% (cls.__name__, label, field, model.__name__))
def check_formfield(cls, model, opts, label, field):
- if hasattr(cls.form, 'base_fields'):
+ if getattr(cls.form, 'base_fields', None):
try:
cls.form.base_fields[field]
except KeyError:
View
0  tests/regressiontests/admin_validation/__init__.py
No changes.
View
43 tests/regressiontests/admin_validation/models.py
@@ -0,0 +1,43 @@
+"""
+Tests of ModelAdmin validation logic.
+"""
+
+from django.db import models
+
+class Song(models.Model):
+ title = models.CharField(max_length=150)
+
+ class Meta:
+ ordering = ('title',)
+
+ def __unicode__(self):
+ return self.title
+
+__test__ = {'API_TESTS':"""
+
+>>> from django import forms
+>>> 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
+
+>>> class ValidFields(admin.ModelAdmin):
+... form = SongForm
+... fields = ['title']
+
+>>> class InvalidFields(admin.ModelAdmin):
+... form = SongForm
+... fields = ['spam']
+
+>>> validate(ValidFields, Song)
+>>> validate(InvalidFields, Song)
+Traceback (most recent call last):
+ ...
+ImproperlyConfigured: 'InvalidFields.fields' refers to field 'spam' that is missing from the form.
+
+"""}
Please sign in to comment.
Something went wrong with that request. Please try again.