summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-02-14 23:44:46 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-02-14 23:44:46 +0000
commit05182053088d3c30fad3fe5cc81db7a12412ae2b (patch)
tree65a1a38c7605bd3623aa17ad34b6086fd484626b /django
parent4c4209b14449554382b541d709749340f9b1a7ae (diff)
Implemented subclassing Forms in newforms
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4506 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/newforms/forms.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 0b90b09e2c..1032d02e35 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -31,10 +31,21 @@ class SortedDictFromList(SortedDict):
return SortedDictFromList([(k, copy.copy(v)) for k, v in self.items()])
class DeclarativeFieldsMetaclass(type):
- "Metaclass that converts Field attributes to a dictionary called 'base_fields'."
+ """
+ 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'] = SortedDictFromList(fields)
return type.__new__(cls, name, bases, attrs)