1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #34148 -- Reverted "Fixed #32901 -- Optimized BaseForm.__getitem__()."

This reverts commit edde2a0699.

Thanks Jan Pieter Waagmeester for the report.
This commit is contained in:
Francesco Panico 2022-11-17 22:45:06 +01:00 committed by Mariusz Felisiak
parent fbde929b19
commit 51faf4bd17
3 changed files with 20 additions and 7 deletions

View File

@ -330,6 +330,7 @@ answer newbie questions, and generally made Django that much better:
Florian Demmer <fdemmer@gmail.com> Florian Demmer <fdemmer@gmail.com>
Florian Moussous <florian.moussous@gmail.com> Florian Moussous <florian.moussous@gmail.com>
Fran Hrženjak <fran.hrzenjak@gmail.com> Fran Hrženjak <fran.hrzenjak@gmail.com>
Francesco Panico <panico.francesco@gmail.com>
Francisco Albarran Cristobal <pahko.xd@gmail.com> Francisco Albarran Cristobal <pahko.xd@gmail.com>
Francisco Couzo <franciscouzo@gmail.com> Francisco Couzo <franciscouzo@gmail.com>
François Freitag <mail@franek.fr> François Freitag <mail@franek.fr>

View File

@ -174,10 +174,6 @@ class BaseForm(RenderableFormMixin):
def __getitem__(self, name): def __getitem__(self, name):
"""Return a BoundField with the given name.""" """Return a BoundField with the given name."""
try:
return self._bound_fields_cache[name]
except KeyError:
pass
try: try:
field = self.fields[name] field = self.fields[name]
except KeyError: except KeyError:
@ -189,9 +185,9 @@ class BaseForm(RenderableFormMixin):
", ".join(sorted(self.fields)), ", ".join(sorted(self.fields)),
) )
) )
bound_field = field.get_bound_field(self, name) if name not in self._bound_fields_cache:
self._bound_fields_cache[name] = bound_field self._bound_fields_cache[name] = field.get_bound_field(self, name)
return bound_field return self._bound_fields_cache[name]
@property @property
def errors(self): def errors(self):

View File

@ -4579,6 +4579,22 @@ Options: <select multiple name="options" required>
'<legend number="9999" for="id_first_name">First name:</legend>', '<legend number="9999" for="id_first_name">First name:</legend>',
) )
def test_remove_cached_field(self):
class TestForm(Form):
name = CharField(max_length=10)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Populate fields cache.
[field for field in self]
# Removed cached field.
del self.fields["name"]
f = TestForm({"name": "abcde"})
with self.assertRaises(KeyError):
f["name"]
@jinja2_tests @jinja2_tests
class Jinja2FormsTestCase(FormsTestCase): class Jinja2FormsTestCase(FormsTestCase):