Ticket #7790: validation.py.patch

File validation.py.patch, 2.3 KB (added by Rozza, 16 years ago)

Patch to validation.py and added test cases to modeladmin/models.py

  • django/contrib/admin/validation.py

     
    244244
    245245def _check_field_exists(cls, model, opts, label, field):
    246246    try:
    247         return opts.get_field(field)
     247        # Check to see if a custom form is being used and has the declared field
     248        if hasattr(cls.form, 'base_fields'):
     249            return cls.form.base_fields[field]
     250        else:
     251            return opts.get_field(field)
     252    except KeyError:
     253        raise ImproperlyConfigured("`%s.%s` refers to "
     254                "field `%s` that is missing from the form `%s`."
     255                % (cls.__name__, label, field, cls.form.__name__))
    248256    except models.FieldDoesNotExist:
    249257        raise ImproperlyConfigured("`%s.%s` refers to "
    250258                "field `%s` that is missing from model `%s`."
  • tests/regressiontests/modeladmin/models.py

     
    262262>>> site = AdminSite()
    263263>>> site.register(ValidationTestModel, ValidationTestModelAdmin)
    264264
     265
     266# Check custom admin forms can validate - using the previous example of the AdminBandForm
     267>>> class AdminBandForm(forms.ModelForm):
     268...     delete = forms.BooleanField()
     269...     
     270...     class Meta:
     271...         model = Band
     272
     273>>> class BandAdmin(ModelAdmin):
     274...     form = AdminBandForm
     275...
     276...     fieldsets = (
     277...         ('Band', {
     278...             'fields' : ('name', 'bio', 'sign_date', 'delete')
     279...         }),
     280...     )
     281
     282>>> validate(BandAdmin, Band)
     283
     284
     285# Ensure it catches invalid form fields - deleted has not be declared
     286>>> class BandAdmin(ModelAdmin):
     287...     form = AdminBandForm
     288...
     289...     fieldsets = (
     290...         ('Band', {
     291...             'fields' : ('name', 'bio', 'sign_date', 'deleted')
     292...         }),
     293...     )
     294
     295>>> validate(BandAdmin, Band)
     296Traceback (most recent call last):
     297...
     298ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `deleted` that is missing from the form `AdminBandForm`.
     299
    265300# raw_id_fields
    266301
    267302>>> class ValidationTestModelAdmin(ModelAdmin):
Back to Top