diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 3196db339e2..3952f6a8fbc 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -2,7 +2,7 @@ Form classes """ -import copy +from copy import deepcopy from django.utils.datastructures import SortedDict from django.utils.html import escape @@ -21,18 +21,6 @@ def pretty_name(name): name = name[0].upper() + name[1:] return name.replace('_', ' ') -class SortedDictFromList(SortedDict): - "A dictionary that keeps its keys in the order in which they're inserted." - # This is different than django.utils.datastructures.SortedDict, because - # this takes a list/tuple as the argument to __init__(). - def __init__(self, data=None): - if data is None: data = [] - self.keyOrder = [d[0] for d in data] - dict.__init__(self, dict(data)) - - def copy(self): - return SortedDictFromList([(k, copy.deepcopy(v)) for k, v in self.items()]) - class DeclarativeFieldsMetaclass(type): """ Metaclass that converts Field attributes to a dictionary called @@ -49,7 +37,7 @@ class DeclarativeFieldsMetaclass(type): if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields - attrs['base_fields'] = SortedDictFromList(fields) + attrs['base_fields'] = SortedDict(fields) return type.__new__(cls, name, bases, attrs) class BaseForm(StrAndUnicode): @@ -74,7 +62,7 @@ class BaseForm(StrAndUnicode): # alter self.fields, we create self.fields here by copying base_fields. # Instances should always modify self.fields; they should not modify # self.base_fields. - self.fields = self.base_fields.copy() + self.fields = deepcopy(self.base_fields) def __unicode__(self): return self.as_table() diff --git a/django/newforms/models.py b/django/newforms/models.py index 00a23573d81..269166c5d51 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -5,9 +5,10 @@ and database field objects. from django.utils.translation import ugettext from django.utils.encoding import smart_unicode +from django.utils.datastructures import SortedDict from util import ValidationError -from forms import BaseForm, SortedDictFromList +from forms import BaseForm from fields import Field, ChoiceField from widgets import Select, SelectMultiple, MultipleHiddenInput @@ -89,7 +90,7 @@ def form_for_model(model, form=BaseForm, fields=None, formfield = formfield_callback(f) if formfield: field_list.append((f.name, formfield)) - base_fields = SortedDictFromList(field_list) + base_fields = SortedDict(field_list) return type(opts.object_name + 'Form', (form,), {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')}) @@ -118,7 +119,7 @@ def form_for_instance(instance, form=BaseForm, fields=None, formfield = formfield_callback(f, initial=current_value) if formfield: field_list.append((f.name, formfield)) - base_fields = SortedDictFromList(field_list) + base_fields = SortedDict(field_list) return type(opts.object_name + 'InstanceForm', (form,), {'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance, fields, 'changed')}) @@ -127,8 +128,8 @@ def form_for_fields(field_list): """ Returns a Form class for the given list of Django database field instances. """ - fields = SortedDictFromList([(f.name, f.formfield()) - for f in field_list if f.editable]) + fields = SortedDict([(f.name, f.formfield()) + for f in field_list if f.editable]) return type('FormForFields', (BaseForm,), {'base_fields': fields}) class QuerySetIterator(object):