Ticket #7790: validation.py.patch
File validation.py.patch, 2.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/validation.py
244 244 245 245 def _check_field_exists(cls, model, opts, label, field): 246 246 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__)) 248 256 except models.FieldDoesNotExist: 249 257 raise ImproperlyConfigured("`%s.%s` refers to " 250 258 "field `%s` that is missing from model `%s`." -
tests/regressiontests/modeladmin/models.py
262 262 >>> site = AdminSite() 263 263 >>> site.register(ValidationTestModel, ValidationTestModelAdmin) 264 264 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) 296 Traceback (most recent call last): 297 ... 298 ImproperlyConfigured: `BandAdmin.fieldsets[0][1]['fields']` refers to field `deleted` that is missing from the form `AdminBandForm`. 299 265 300 # raw_id_fields 266 301 267 302 >>> class ValidationTestModelAdmin(ModelAdmin):