Code

Ticket #7837: forms_metaclass_hierarchy_problem_fix.diff

File forms_metaclass_hierarchy_problem_fix.diff, 3.6 KB (added by msaelices, 6 years ago)

This patch fix problems

Line 
1Index: django/forms/models.py
2===================================================================
3--- django/forms/models.py      (revisión: 7972)
4+++ django/forms/models.py      (copia de trabajo)
5@@ -230,7 +230,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: 7972)
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: 7972)
36+++ tests/regressiontests/forms/models.py       (copia de trabajo)
37@@ -15,10 +15,10 @@
38     name = models.CharField(max_length=10)
39 
40 __test__ = {'API_TESTS': """
41->>> from django.forms import form_for_model, form_for_instance
42+>>> from django import forms
43 
44 # Boundary conditions on a PostitiveIntegerField #########################
45->>> BoundaryForm = form_for_model(BoundaryModel)
46+>>> BoundaryForm = forms.form_for_model(BoundaryModel)
47 >>> f = BoundaryForm({'positive_integer':100})
48 >>> f.is_valid()
49 True
50@@ -32,7 +32,7 @@
51 # Formfield initial values ########
52 If the model has default values for some fields, they are used as the formfield
53 initial values.
54->>> DefaultsForm = form_for_model(Defaults)
55+>>> DefaultsForm = forms.form_for_model(Defaults)
56 >>> DefaultsForm().fields['name'].initial
57 u'class default value'
58 >>> DefaultsForm().fields['def_date'].initial
59@@ -43,11 +43,28 @@
60 In form_for_instance(), the initial values come from the instance's values, not
61 the model's defaults.
62 >>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12)
63->>> InstanceForm = form_for_instance(foo_instance)
64+>>> InstanceForm = forms.form_for_instance(foo_instance)
65 >>> InstanceForm().fields['name'].initial
66 u'instance value'
67 >>> InstanceForm().fields['def_date'].initial
68 datetime.date(1969, 4, 4)
69 >>> InstanceForm().fields['value'].initial
70 12
71+
72+# Regression test for #6926 ##################################################
73+
74+Make sure hierarchy in metaclasses doesn't miss declared fields
75+
76+>>> class ExtraFieldForm(forms.Form):
77+...     extra_field = forms.CharField()
78+
79+>>> class MixinForm(ExtraFieldForm, forms.ModelForm):
80+...    # this is necessary to avoid python metaclass conflict error
81+...    __metaclass__ = type('FooFormMetaclass', (ExtraFieldForm.__metaclass__, forms.ModelForm.__metaclass__), {})
82+...    non_existing_field = forms.CharField()
83+...    class Meta:
84+...        model = ChoiceModel
85+
86+>>> MixinForm.base_fields.keys()
87+['name', 'extra_field', 'non_existing_field']
88 """}