Ticket #7837: forms_metaclass_hierarchy_problem_fix.4.diff
File forms_metaclass_hierarchy_problem_fix.4.diff, 3.6 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
230 230 231 231 if 'media' not in attrs: 232 232 new_class.media = media_property(new_class) 233 declared_fields = get_declared_fields(bases, attrs, False) 233 if attrs.has_key('declared_fields'): 234 declared_fields = attrs['declared_fields'] 235 else: 236 declared_fields = SortedDict() 237 declared_fields.update(get_declared_fields(bases, attrs, False)) 234 238 opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) 235 239 if opts.model: 236 240 # 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
15 15 name = models.CharField(max_length=10) 16 16 17 17 __test__ = {'API_TESTS': """ 18 >>> from django .forms import form_for_model, form_for_instance18 >>> from django import forms 19 19 20 20 # Boundary conditions on a PostitiveIntegerField ######################### 21 >>> BoundaryForm = form _for_model(BoundaryModel)21 >>> BoundaryForm = forms.form_for_model(BoundaryModel) 22 22 >>> f = BoundaryForm({'positive_integer':100}) 23 23 >>> f.is_valid() 24 24 True … … 32 32 # Formfield initial values ######## 33 33 If the model has default values for some fields, they are used as the formfield 34 34 initial values. 35 >>> DefaultsForm = form _for_model(Defaults)35 >>> DefaultsForm = forms.form_for_model(Defaults) 36 36 >>> DefaultsForm().fields['name'].initial 37 37 u'class default value' 38 38 >>> DefaultsForm().fields['def_date'].initial … … 43 43 In form_for_instance(), the initial values come from the instance's values, not 44 44 the model's defaults. 45 45 >>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12) 46 >>> InstanceForm = form _for_instance(foo_instance)46 >>> InstanceForm = forms.form_for_instance(foo_instance) 47 47 >>> InstanceForm().fields['name'].initial 48 48 u'instance value' 49 49 >>> InstanceForm().fields['def_date'].initial 50 50 datetime.date(1969, 4, 4) 51 51 >>> InstanceForm().fields['value'].initial 52 52 12 53 54 # Regression test for #7837 ################################################## 55 56 Make sure hierarchy in metaclasses doesn't miss declared fields 57 58 >>> class ExtraFieldForm(forms.Form): 59 ... extra_field = forms.CharField() 60 61 >>> class ChildForm(ExtraFieldForm, forms.ModelForm): 62 ... # this is necessary to avoid python metaclass conflict error 63 ... __metaclass__ = type('ChildFormMetaclass', (ExtraFieldForm.__metaclass__, forms.ModelForm.__metaclass__), {}) 64 ... non_existing_field = forms.CharField() 65 ... class Meta: 66 ... model = ChoiceModel 67 68 >>> ChildForm.base_fields.keys() 69 ['name', 'extra_field', 'non_existing_field'] 53 70 """}