1. Luke Plant
  2. django

Commits

bro...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed f75a6e0

newforms-admin: Fixed #7790 -- Check form fields, not model fields when testing for field existence in fields and fieldsets. Thanks Rozza for catching this and writing an initial patch.

  • Participants
  • Parent commits 99f476c
  • Branches newforms-admin

Comments (0)

Files changed (2)

File django/contrib/admin/validation.py

View file
  • Ignore whitespace
 
 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
 
 
     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'):
     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__)
 
                         "%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):
                 "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)

File tests/regressiontests/modeladmin/models.py

View file
  • Ignore whitespace
 >>> 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",)}),)
 ...
 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):
 >>> 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