Skip to content

Commit

Permalink
Fixed #5918 -- Removed SortedDictFromList since SortedDict now ca…
Browse files Browse the repository at this point in the history
…n do everything `SortedDictFromList` could do. Since `SortedDict`'s `copy` method doesn't return a deepcopy as `SortedDictFromList`'s `copy` method did, you will need to update your code if you were relying on `SortedDictFromList.copy` to return a deepcopy by using the `deepcopy` function from the `copy` module.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6668 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
gdub committed Nov 11, 2007
1 parent af256a0 commit a4907be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
18 changes: 3 additions & 15 deletions django/newforms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Form classes
"""

import copy
from copy import deepcopy

from django.utils.datastructures import SortedDict
from django.utils.html import escape
Expand All @@ -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
Expand All @@ -49,7 +37,7 @@ def __new__(cls, name, bases, attrs):
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):
Expand All @@ -74,7 +62,7 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
# 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()
Expand Down
11 changes: 6 additions & 5 deletions django/newforms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

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

Expand Down Expand Up @@ -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')})
Expand Down Expand Up @@ -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')})
Expand All @@ -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):
Expand Down

0 comments on commit a4907be

Please sign in to comment.