Changeset 7943
- Timestamp:
- 07/17/08 11:48:27 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/newforms-admin/django/contrib/admin/validation.py
r7942 r7943 2 2 from django.core.exceptions import ImproperlyConfigured 3 3 from django.db import models 4 from django.newforms.models import BaseModelForm, BaseModelFormSet 4 from django.newforms.models import BaseModelForm, BaseModelFormSet, fields_for_model 5 5 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 6 6 from django.contrib.admin.options import HORIZONTAL, VERTICAL … … 145 145 def _check_field_existsw(label, field): 146 146 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) 147 150 148 151 # raw_id_fields … … 160 163 _check_istuplew('fields', cls.fields) 161 164 for field in cls.fields: 162 _check_f ield_existsw('fields', field)165 _check_form_field_existsw('fields', field) 163 166 if cls.fieldsets: 164 167 raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__) … … 178 181 % (cls.__name__, idx)) 179 182 for field in flatten_fieldsets(cls.fieldsets): 180 _check_f ield_existsw("fieldsets[%d][1]['fields']" % idx, field)183 _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field) 181 184 182 185 # form … … 251 254 % (cls.__name__, label, field, model.__name__)) 252 255 256 def _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 253 271 def _check_attr_exists(cls, model, opts, label, field): 254 272 try: django/branches/newforms-admin/tests/regressiontests/modeladmin/models.py
r7942 r7943 333 333 Traceback (most recent call last): 334 334 ... 335 ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from model `ValidationTestModel`.335 ImproperlyConfigured: `ValidationTestModelAdmin.fieldsets[0][1]['fields']` refers to field `non_existent_field` that is missing from the form. 336 336 337 337 >>> class ValidationTestModelAdmin(ModelAdmin): … … 358 358 ImproperlyConfigured: ValidationTestModelAdmin.form does not inherit from BaseModelForm. 359 359 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) 369 Traceback (most recent call last): 370 ... 371 ImproperlyConfigured: `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) 393 Traceback (most recent call last): 394 ... 395 ImproperlyConfigured: `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 360 412 # filter_vertical 361 413 … … 737 789 Traceback (most recent call last): 738 790 ... 739 ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from model `ValidationTestInlineModel`.791 ImproperlyConfigured: `ValidationTestInline.fields` refers to field `non_existent_field` that is missing from the form. 740 792 741 793 # fk_name
