Ticket #7837: forms_metaclass_hierarchy_problem_fix_r8049.diff
File forms_metaclass_hierarchy_problem_fix_r8049.diff, 3.9 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
229 229 230 230 if 'media' not in attrs: 231 231 new_class.media = media_property(new_class) 232 declared_fields = get_declared_fields(bases, attrs, False) 232 if attrs.has_key('declared_fields'): 233 declared_fields = attrs['declared_fields'] 234 else: 235 declared_fields = SortedDict() 236 declared_fields.update(get_declared_fields(bases, attrs, False)) 233 237 opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) 234 238 if opts.model: 235 239 # If a model is defined, extract form fields from it. -
django/forms/forms.py
56 56 'base_fields', taking into account parent class 'base_fields' as well. 57 57 """ 58 58 def __new__(cls, name, bases, attrs): 59 attrs['base_fields'] = get_declared_fields(bases, attrs) 59 declared_fields = get_declared_fields(bases, attrs) 60 attrs['declared_fields'] = declared_fields 61 attrs['base_fields'] = declared_fields 60 62 new_class = super(DeclarativeFieldsMetaclass, 61 63 cls).__new__(cls, name, bases, attrs) 62 64 if 'media' not in attrs: -
tests/regressiontests/forms/models.py
25 25 file1 = django_forms.FileField() 26 26 27 27 __test__ = {'API_TESTS': """ 28 >>> from django.forms import form_for_model, form_for_instance29 28 >>> from django.core.files.uploadedfile import SimpleUploadedFile 30 29 31 30 # FileModel with unicode filename and data ######################### … … 37 36 >>> m = FileModel.objects.create(file=f.cleaned_data['file1']) 38 37 39 38 # Boundary conditions on a PostitiveIntegerField ######################### 40 >>> BoundaryForm = form_for_model(BoundaryModel)39 >>> BoundaryForm = django_forms.form_for_model(BoundaryModel) 41 40 >>> f = BoundaryForm({'positive_integer':100}) 42 41 >>> f.is_valid() 43 42 True … … 51 50 # Formfield initial values ######## 52 51 If the model has default values for some fields, they are used as the formfield 53 52 initial values. 54 >>> DefaultsForm = form_for_model(Defaults)53 >>> DefaultsForm = django_forms.form_for_model(Defaults) 55 54 >>> DefaultsForm().fields['name'].initial 56 55 u'class default value' 57 56 >>> DefaultsForm().fields['def_date'].initial … … 62 61 In form_for_instance(), the initial values come from the instance's values, not 63 62 the model's defaults. 64 63 >>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12) 65 >>> InstanceForm = form_for_instance(foo_instance)64 >>> InstanceForm = django_forms.form_for_instance(foo_instance) 66 65 >>> InstanceForm().fields['name'].initial 67 66 u'instance value' 68 67 >>> InstanceForm().fields['def_date'].initial 69 68 datetime.date(1969, 4, 4) 70 69 >>> InstanceForm().fields['value'].initial 71 70 12 71 72 # Regression test for #7837 ################################################## 73 74 Make sure hierarchy in metaclasses doesn't miss declared fields 75 76 >>> class ExtraFieldForm(django_forms.Form): 77 ... extra_field = django_forms.CharField() 78 79 >>> class ChildForm(ExtraFieldForm, django_forms.ModelForm): 80 ... # this is necessary to avoid python metaclass conflict error 81 ... __metaclass__ = type('ChildFormMetaclass', (ExtraFieldForm.__metaclass__, django_forms.ModelForm.__metaclass__), {}) 82 ... non_existing_field = django_forms.CharField() 83 ... class Meta: 84 ... model = ChoiceModel 85 86 >>> ChildForm.base_fields.keys() 87 ['name', 'extra_field', 'non_existing_field'] 72 88 """}