mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #30159 -- Removed unneeded use of OrderedDict.
Dicts preserve order since Python 3.6.
This commit is contained in:
@@ -3,7 +3,6 @@ Form classes
|
||||
"""
|
||||
|
||||
import copy
|
||||
from collections import OrderedDict
|
||||
|
||||
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
|
||||
# BoundField is imported for backwards compatibility in Django 1.9
|
||||
@@ -31,12 +30,12 @@ class DeclarativeFieldsMetaclass(MediaDefiningClass):
|
||||
if isinstance(value, Field):
|
||||
current_fields.append((key, value))
|
||||
attrs.pop(key)
|
||||
attrs['declared_fields'] = OrderedDict(current_fields)
|
||||
attrs['declared_fields'] = dict(current_fields)
|
||||
|
||||
new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs)
|
||||
|
||||
# Walk through the MRO.
|
||||
declared_fields = OrderedDict()
|
||||
declared_fields = {}
|
||||
for base in reversed(new_class.__mro__):
|
||||
# Collect fields from base class.
|
||||
if hasattr(base, 'declared_fields'):
|
||||
@@ -52,11 +51,6 @@ 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:
|
||||
@@ -129,7 +123,7 @@ class BaseForm:
|
||||
"""
|
||||
if field_order is None:
|
||||
return
|
||||
fields = OrderedDict()
|
||||
fields = {}
|
||||
for key in field_order:
|
||||
try:
|
||||
fields[key] = self.fields.pop(key)
|
||||
|
||||
@@ -3,7 +3,6 @@ Helper functions for creating Form classes from Django models
|
||||
and database field objects.
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
from itertools import chain
|
||||
|
||||
from django.core.exceptions import (
|
||||
@@ -105,7 +104,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None,
|
||||
labels=None, help_texts=None, error_messages=None,
|
||||
field_classes=None, *, apply_limit_choices_to=True):
|
||||
"""
|
||||
Return an ``OrderedDict`` containing form fields for the given model.
|
||||
Return a dictionary containing form fields for the given model.
|
||||
|
||||
``fields`` is an optional list of field names. If provided, return only the
|
||||
named fields.
|
||||
@@ -134,7 +133,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None,
|
||||
``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
|
||||
should be applied to a field's queryset.
|
||||
"""
|
||||
field_list = []
|
||||
field_dict = {}
|
||||
ignored = []
|
||||
opts = model._meta
|
||||
# Avoid circular import
|
||||
@@ -178,15 +177,14 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None,
|
||||
if formfield:
|
||||
if apply_limit_choices_to:
|
||||
apply_limit_choices_to_to_formfield(formfield)
|
||||
field_list.append((f.name, formfield))
|
||||
field_dict[f.name] = formfield
|
||||
else:
|
||||
ignored.append(f.name)
|
||||
field_dict = OrderedDict(field_list)
|
||||
if fields:
|
||||
field_dict = OrderedDict(
|
||||
[(f, field_dict.get(f)) for f in fields
|
||||
if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
|
||||
)
|
||||
field_dict = {
|
||||
f: field_dict.get(f) for f in fields
|
||||
if (not exclude or f not in exclude) and f not in ignored
|
||||
}
|
||||
return field_dict
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user