Code

Ticket #3632: forms.diff

File forms.diff, 1.7 KB (added by ludo@…, 7 years ago)

tentative patch to newforms/forms.py

Line 
1Index: newforms/forms.py
2===================================================================
3--- newforms/forms.py   (revision 4637)
4+++ newforms/forms.py   (working copy)
5@@ -36,17 +36,20 @@
6     'base_fields', taking into account parent class 'base_fields' as well.
7     """
8     def __new__(cls, name, bases, attrs):
9-        fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
10-        fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
11+        if 'base_fields' in attrs:
12+            fields = attrs['base_fields']
13+        else:
14+            fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
15+            fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
16 
17-        # If this class is subclassing another Form, add that Form's fields.
18-        # Note that we loop over the bases in *reverse*. This is necessary in
19-        # order to preserve the correct order of fields.
20-        for base in bases[::-1]:
21-            if hasattr(base, 'base_fields'):
22-                fields = base.base_fields.items() + fields
23+            # If this class is subclassing another Form, add that Form's fields.
24+            # Note that we loop over the bases in *reverse*. This is necessary in
25+            # order to preserve the correct order of fields.
26+            for base in bases[::-1]:
27+                if hasattr(base, 'base_fields'):
28+                    fields = base.base_fields.items() + fields
29 
30-        attrs['base_fields'] = SortedDictFromList(fields)
31+            attrs['base_fields'] = SortedDictFromList(fields)
32         return type.__new__(cls, name, bases, attrs)
33 
34 class BaseForm(StrAndUnicode):