From e07e743e0c375b380748561d18b975c8211a4a01 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 27 Jan 2017 12:46:21 -0500 Subject: [PATCH] Refs #23919 -- Used DeclarativeFieldsMetaclass.__prepare__() for tracking form field order. --- django/forms/fields.py | 7 ------- django/forms/forms.py | 6 +++++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index e12702f713..979a13835d 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -54,9 +54,6 @@ class Field: } 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, help_text='', error_messages=None, show_hidden_initial=False, validators=(), localize=False, disabled=False, label_suffix=None): @@ -109,10 +106,6 @@ class Field: self.widget = widget - # Increase the creation counter, and save our local copy. - self.creation_counter = Field.creation_counter - Field.creation_counter += 1 - messages = {} for c in reversed(self.__class__.__mro__): messages.update(getattr(c, 'default_error_messages', {})) diff --git a/django/forms/forms.py b/django/forms/forms.py index 7fccdc2b65..5770f6d609 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -34,7 +34,6 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass): if isinstance(value, Field): current_fields.append((key, value)) attrs.pop(key) - current_fields.sort(key=lambda x: x[1].creation_counter) attrs['declared_fields'] = OrderedDict(current_fields) new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs) @@ -56,6 +55,11 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass): return new_class + @classmethod + def __prepare__(metacls, name, bases, **kwds): + # Remember the order in which form fields are defined. + return OrderedDict() + @html_safe class BaseForm: