Code

Ticket #7837: forms_metaclass_hierarchy_problem_fix_r8049.diff

File forms_metaclass_hierarchy_problem_fix_r8049.diff, 3.9 KB (added by msaelices, 6 years ago)

Updated patch for [8049]

Line 
1Index: django/forms/models.py
2===================================================================
3--- django/forms/models.py      (revisión: 8049)
4+++ django/forms/models.py      (copia de trabajo)
5@@ -229,7 +229,11 @@
6 
7         if 'media' not in attrs:
8             new_class.media = media_property(new_class)
9-        declared_fields = get_declared_fields(bases, attrs, False)
10+        if attrs.has_key('declared_fields'):
11+            declared_fields = attrs['declared_fields']
12+        else:
13+            declared_fields = SortedDict()
14+        declared_fields.update(get_declared_fields(bases, attrs, False))
15         opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None))
16         if opts.model:
17             # If a model is defined, extract form fields from it.
18Index: django/forms/forms.py
19===================================================================
20--- django/forms/forms.py       (revisión: 8049)
21+++ django/forms/forms.py       (copia de trabajo)
22@@ -56,7 +56,9 @@
23     'base_fields', taking into account parent class 'base_fields' as well.
24     """
25     def __new__(cls, name, bases, attrs):
26-        attrs['base_fields'] = get_declared_fields(bases, attrs)
27+        declared_fields = get_declared_fields(bases, attrs)
28+        attrs['declared_fields'] = declared_fields
29+        attrs['base_fields'] = declared_fields
30         new_class = super(DeclarativeFieldsMetaclass,
31                      cls).__new__(cls, name, bases, attrs)
32         if 'media' not in attrs:
33Index: tests/regressiontests/forms/models.py
34===================================================================
35--- tests/regressiontests/forms/models.py       (revisión: 8049)
36+++ tests/regressiontests/forms/models.py       (copia de trabajo)
37@@ -25,7 +25,6 @@
38     file1 = django_forms.FileField()
39 
40 __test__ = {'API_TESTS': """
41->>> from django.forms import form_for_model, form_for_instance
42 >>> from django.core.files.uploadedfile import SimpleUploadedFile
43 
44 # FileModel with unicode filename and data #########################
45@@ -37,7 +36,7 @@
46 >>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
47 
48 # Boundary conditions on a PostitiveIntegerField #########################
49->>> BoundaryForm = form_for_model(BoundaryModel)
50+>>> BoundaryForm = django_forms.form_for_model(BoundaryModel)
51 >>> f = BoundaryForm({'positive_integer':100})
52 >>> f.is_valid()
53 True
54@@ -51,7 +50,7 @@
55 # Formfield initial values ########
56 If the model has default values for some fields, they are used as the formfield
57 initial values.
58->>> DefaultsForm = form_for_model(Defaults)
59+>>> DefaultsForm = django_forms.form_for_model(Defaults)
60 >>> DefaultsForm().fields['name'].initial
61 u'class default value'
62 >>> DefaultsForm().fields['def_date'].initial
63@@ -62,11 +61,28 @@
64 In form_for_instance(), the initial values come from the instance's values, not
65 the model's defaults.
66 >>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12)
67->>> InstanceForm = form_for_instance(foo_instance)
68+>>> InstanceForm = django_forms.form_for_instance(foo_instance)
69 >>> InstanceForm().fields['name'].initial
70 u'instance value'
71 >>> InstanceForm().fields['def_date'].initial
72 datetime.date(1969, 4, 4)
73 >>> InstanceForm().fields['value'].initial
74 12
75+
76+# Regression test for #7837 ##################################################
77+
78+Make sure hierarchy in metaclasses doesn't miss declared fields
79+
80+>>> class ExtraFieldForm(django_forms.Form):
81+...     extra_field = django_forms.CharField()
82+
83+>>> class ChildForm(ExtraFieldForm, django_forms.ModelForm):
84+...    # this is necessary to avoid python metaclass conflict error
85+...    __metaclass__ = type('ChildFormMetaclass', (ExtraFieldForm.__metaclass__, django_forms.ModelForm.__metaclass__), {})
86+...    non_existing_field = django_forms.CharField()
87+...    class Meta:
88+...        model = ChoiceModel
89+
90+>>> ChildForm.base_fields.keys()
91+['name', 'extra_field', 'non_existing_field']
92 """}