Django

Code

Changeset 7943

Show
Ignore:
Timestamp:
07/17/08 11:48:27 (4 months ago)
Author:
brosner
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/contrib/admin/validation.py

    r7942 r7943  
    22from django.core.exceptions import ImproperlyConfigured 
    33from django.db import models 
    4 from django.newforms.models import BaseModelForm, BaseModelFormSet 
     4from django.newforms.models import BaseModelForm, BaseModelFormSet, fields_for_model 
    55from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 
    66from django.contrib.admin.options import HORIZONTAL, VERTICAL 
     
    145145    def _check_field_existsw(label, field): 
    146146        return _check_field_exists(cls, model, opts, label, field) 
     147     
     148    def _check_form_field_existsw(label, field): 
     149        return _check_form_field_exists(cls, model, opts, label, field) 
    147150 
    148151    # raw_id_fields 
     
    160163        _check_istuplew('fields', cls.fields) 
    161164        for field in cls.fields: 
    162             _check_field_existsw('fields', field) 
     165            _check_form_field_existsw('fields', field) 
    163166        if cls.fieldsets: 
    164167            raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__) 
     
    178181                        % (cls.__name__, idx)) 
    179182        for field in flatten_fieldsets(cls.fieldsets): 
    180             _check_field_existsw("fieldsets[%d][1]['fields']" % idx, field) 
     183            _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field) 
    181184 
    182185    # form 
     
    251254                % (cls.__name__, label, field, model.__name__)) 
    252255 
     256def _check_form_field_exists(cls, model, opts, label, field): 
     257    if hasattr(cls.form, 'base_fields'): 
     258        try: 
     259            cls.form.base_fields[field] 
     260        except KeyError: 
     261            raise ImproperlyConfigured("`%s.%s` refers to field `%s` that " 
     262                "is missing from the form." % (cls.__name__, label, field)) 
     263    else: 
     264        fields = fields_for_model(model) 
     265        try: 
     266            fields[field] 
     267        except KeyError: 
     268            raise ImproperlyConfigured("`%s.%s` refers to field `%s` that " 
     269                "is missing from the form." % (cls.__name__, label, field)) 
     270 
    253271def _check_attr_exists(cls, model, opts, label, field): 
    254272    try: 
  • django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py

    r7942 r7943  
    333333Traceback (most recent call last): 
    334334... 
    335 ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from model `ValidationTestModel`
     335ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form
    336336 
    337337>>> class ValidationTestModelAdmin(ModelAdmin): 
     
    358358ImproperlyConfigured: ValidationTestModelAdmin.form does not inherit from BaseModelForm. 
    359359 
     360# fielsets with custom form 
     361 
     362>>> class BandAdmin(ModelAdmin): 
     363...     fieldsets = ( 
     364...         ('Band', { 
     365...             'fields': ('non_existent_field',) 
     366...         }), 
     367...     ) 
     368>>> validate(BandAdmin, Band) 
     369Traceback (most recent call last): 
     370... 
     371ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form. 
     372 
     373>>> class BandAdmin(ModelAdmin): 
     374...     fieldsets = ( 
     375...         ('Band', { 
     376...             'fields': ('name',) 
     377...         }), 
     378...     ) 
     379>>> validate(BandAdmin, Band) 
     380 
     381>>> class AdminBandForm(forms.ModelForm): 
     382...     class Meta: 
     383...         model = Band 
     384>>> class BandAdmin(ModelAdmin): 
     385...     form = AdminBandForm 
     386... 
     387...     fieldsets = ( 
     388...         ('Band', { 
     389...             'fields': ('non_existent_field',) 
     390...         }), 
     391...     ) 
     392>>> validate(BandAdmin, Band) 
     393Traceback (most recent call last): 
     394... 
     395ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form. 
     396 
     397>>> class AdminBandForm(forms.ModelForm): 
     398...     delete = forms.BooleanField() 
     399... 
     400...     class Meta: 
     401...         model = Band 
     402>>> class BandAdmin(ModelAdmin): 
     403...     form = AdminBandForm 
     404... 
     405...     fieldsets = ( 
     406...         ('Band', { 
     407...             'fields': ('name', 'bio', 'sign_date', 'delete') 
     408...         }), 
     409...     ) 
     410>>> validate(BandAdmin, Band) 
     411 
    360412# filter_vertical 
    361413 
     
    737789Traceback (most recent call last): 
    738790... 
    739 ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from model `ValidationTestInlineModel`
     791ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from the form
    740792 
    741793# fk_name