Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

newforms-admin: Fixed #7790 -- Check form fields, not model fields wh…

…en testing for field existence in fields and fieldsets. Thanks Rozza for catching this and writing an initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7943 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1eed9b076c28f3cd1720743cfff5a31fcb5aef5d 1 parent 81b1853
@brosner brosner authored
View
24 django/contrib/admin/validation.py
@@ -1,7 +1,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.db import models
-from django.newforms.models import BaseModelForm, BaseModelFormSet
+from django.newforms.models import BaseModelForm, BaseModelFormSet, fields_for_model
from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
from django.contrib.admin.options import HORIZONTAL, VERTICAL
@@ -144,6 +144,9 @@ def _check_isdictw(label, obj):
def _check_field_existsw(label, field):
return _check_field_exists(cls, model, opts, label, field)
+
+ def _check_form_field_existsw(label, field):
+ return _check_form_field_exists(cls, model, opts, label, field)
# raw_id_fields
if hasattr(cls, 'raw_id_fields'):
@@ -159,7 +162,7 @@ def _check_field_existsw(label, field):
if cls.fields: # default value is None
_check_istuplew('fields', cls.fields)
for field in cls.fields:
- _check_field_existsw('fields', field)
+ _check_form_field_existsw('fields', field)
if cls.fieldsets:
raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__)
@@ -177,7 +180,7 @@ def _check_field_existsw(label, field):
"%s.fieldsets[%d][1] field options dict."
% (cls.__name__, idx))
for field in flatten_fieldsets(cls.fieldsets):
- _check_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
+ _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
# form
if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm):
@@ -250,6 +253,21 @@ def _check_field_exists(cls, model, opts, label, field):
"field `%s` that is missing from model `%s`."
% (cls.__name__, label, field, model.__name__))
+def _check_form_field_exists(cls, model, opts, label, field):
+ if hasattr(cls.form, 'base_fields'):
+ try:
+ cls.form.base_fields[field]
+ except KeyError:
+ raise ImproperlyConfigured("`%s.%s` refers to field `%s` that "
+ "is missing from the form." % (cls.__name__, label, field))
+ else:
+ fields = fields_for_model(model)
+ try:
+ fields[field]
+ except KeyError:
+ raise ImproperlyConfigured("`%s.%s` refers to field `%s` that "
+ "is missing from the form." % (cls.__name__, label, field))
+
def _check_attr_exists(cls, model, opts, label, field):
try:
return opts.get_field(field)
View
56 tests/regressiontests/modeladmin/models.py
@@ -332,7 +332,7 @@ class and an AdminSite instance, so let's just go ahead and do that manually
>>> validate(ValidationTestModelAdmin, ValidationTestModel)
Traceback (most recent call last):
...
-ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from model `ValidationTestModel`.
+ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form.
>>> class ValidationTestModelAdmin(ModelAdmin):
... fieldsets = (("General", {"fields": ("name",)}),)
@@ -357,6 +357,58 @@ class and an AdminSite instance, so let's just go ahead and do that manually
...
ImproperlyConfigured: ValidationTestModelAdmin.form does not inherit from BaseModelForm.
+# fielsets with custom form
+
+>>> class BandAdmin(ModelAdmin):
+... fieldsets = (
+... ('Band', {
+... 'fields': ('non_existent_field',)
+... }),
+... )
+>>> validate(BandAdmin, Band)
+Traceback (most recent call last):
+...
+ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form.
+
+>>> class BandAdmin(ModelAdmin):
+... fieldsets = (
+... ('Band', {
+... 'fields': ('name',)
+... }),
+... )
+>>> validate(BandAdmin, Band)
+
+>>> class AdminBandForm(forms.ModelForm):
+... class Meta:
+... model = Band
+>>> class BandAdmin(ModelAdmin):
+... form = AdminBandForm
+...
+... fieldsets = (
+... ('Band', {
+... 'fields': ('non_existent_field',)
+... }),
+... )
+>>> validate(BandAdmin, Band)
+Traceback (most recent call last):
+...
+ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form.
+
+>>> class AdminBandForm(forms.ModelForm):
+... delete = forms.BooleanField()
+...
+... class Meta:
+... model = Band
+>>> class BandAdmin(ModelAdmin):
+... form = AdminBandForm
+...
+... fieldsets = (
+... ('Band', {
+... 'fields': ('name', 'bio', 'sign_date', 'delete')
+... }),
+... )
+>>> validate(BandAdmin, Band)
+
# filter_vertical
>>> class ValidationTestModelAdmin(ModelAdmin):
@@ -736,7 +788,7 @@ class and an AdminSite instance, so let's just go ahead and do that manually
>>> validate(ValidationTestModelAdmin, ValidationTestModel)
Traceback (most recent call last):
...
-ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from model `ValidationTestInlineModel`.
+ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from the form.
# fk_name
Please sign in to comment.
Something went wrong with that request. Please try again.