Author: brosner
Date: 2008-07-17 11:48:27 -0500 (Thu, 17 Jul 2008)
New Revision: 7943

Modified:
   django/branches/newforms-admin/django/contrib/admin/validation.py
   django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py
Log:
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.

Modified: django/branches/newforms-admin/django/contrib/admin/validation.py
===================================================================
--- django/branches/newforms-admin/django/contrib/admin/validation.py   
2008-07-17 16:11:49 UTC (rev 7942)
+++ django/branches/newforms-admin/django/contrib/admin/validation.py   
2008-07-17 16:48:27 UTC (rev 7943)
@@ -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_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 @@
     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 @@
                         "%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 @@
                 "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)

Modified: 
django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py
===================================================================
--- django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py   
2008-07-17 16:11:49 UTC (rev 7942)
+++ django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py   
2008-07-17 16:48:27 UTC (rev 7943)
@@ -332,7 +332,7 @@
 >>> 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 @@
 ...
 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 @@
 >>> 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
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to