summaryrefslogtreecommitdiff
path: root/django/newforms/forms.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-02-14 12:56:49 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-02-14 12:56:49 +0000
commit37962ecea79cb9d296645a5324cd91818fed65b3 (patch)
tree67225fd54f15c13c0a014759a41637bd3931488d /django/newforms/forms.py
parent5078010a31ea32a6fd2e9c4fce5354b8f838b163 (diff)
Fixed #6337. Refs #3632 -- Fixed ModelForms subclassing, to the extent that it can be made to work.
This ended up being an almost complete rewrite of ModelForms.__new__, but should be backwards compatible (although the text of one error message has changed, which is only user visible and only if you pass in invalid code). Documentation updated, also. This started out as a patch from semenov (many thanks!), but by the time all the problems were hammered out, little of the original was left. Still, it was a good starting point. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7112 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms/forms.py')
-rw-r--r--django/newforms/forms.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 04bf62e445..7bc5e2ac02 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -22,23 +22,26 @@ def pretty_name(name):
name = name[0].upper() + name[1:]
return name.replace('_', ' ')
+def get_declared_fields(bases, attrs):
+ fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
+ fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
+
+ # If this class is subclassing another Form, add that Form's fields.
+ # Note that we loop over the bases in *reverse*. This is necessary in
+ # order to preserve the correct order of fields.
+ for base in bases[::-1]:
+ if hasattr(base, 'base_fields'):
+ fields = base.base_fields.items() + fields
+
+ return SortedDict(fields)
+
class DeclarativeFieldsMetaclass(type):
"""
Metaclass that converts Field attributes to a dictionary called
'base_fields', taking into account parent class 'base_fields' as well.
"""
def __new__(cls, name, bases, attrs):
- fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
- fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
-
- # If this class is subclassing another Form, add that Form's fields.
- # Note that we loop over the bases in *reverse*. This is necessary in
- # order to preserve the correct order of fields.
- for base in bases[::-1]:
- if hasattr(base, 'base_fields'):
- fields = base.base_fields.items() + fields
-
- attrs['base_fields'] = SortedDict(fields)
+ attrs['base_fields'] = get_declared_fields(bases, attrs)
return type.__new__(cls, name, bases, attrs)
class BaseForm(StrAndUnicode):