Refs #23919 -- Used DeclarativeFieldsMetaclass.__prepare__() for tracking form field order.

This commit is contained in:
Tim Graham 2017-01-27 12:46:21 -05:00 committed by GitHub
parent d2e40dd8c2
commit e07e743e0c
2 changed files with 5 additions and 8 deletions

View File

@ -54,9 +54,6 @@ class Field:
} }
empty_values = list(validators.EMPTY_VALUES) empty_values = list(validators.EMPTY_VALUES)
# Tracks each time a Field instance is created. Used to retain order.
creation_counter = 0
def __init__(self, required=True, widget=None, label=None, initial=None, def __init__(self, required=True, widget=None, label=None, initial=None,
help_text='', error_messages=None, show_hidden_initial=False, help_text='', error_messages=None, show_hidden_initial=False,
validators=(), localize=False, disabled=False, label_suffix=None): validators=(), localize=False, disabled=False, label_suffix=None):
@ -109,10 +106,6 @@ class Field:
self.widget = widget self.widget = widget
# Increase the creation counter, and save our local copy.
self.creation_counter = Field.creation_counter
Field.creation_counter += 1
messages = {} messages = {}
for c in reversed(self.__class__.__mro__): for c in reversed(self.__class__.__mro__):
messages.update(getattr(c, 'default_error_messages', {})) messages.update(getattr(c, 'default_error_messages', {}))

View File

@ -34,7 +34,6 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass):
if isinstance(value, Field): if isinstance(value, Field):
current_fields.append((key, value)) current_fields.append((key, value))
attrs.pop(key) attrs.pop(key)
current_fields.sort(key=lambda x: x[1].creation_counter)
attrs['declared_fields'] = OrderedDict(current_fields) attrs['declared_fields'] = OrderedDict(current_fields)
new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs) new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs)
@ -56,6 +55,11 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass):
return new_class return new_class
@classmethod
def __prepare__(metacls, name, bases, **kwds):
# Remember the order in which form fields are defined.
return OrderedDict()
@html_safe @html_safe
class BaseForm: class BaseForm: