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 Moussous <florian.moussous@gmail.com>
Fran Hrženjak <fran.hrzenjak@gmail.com>
Francesco Panico <panico.francesco@gmail.com>
Francisco Albarran Cristobal <pahko.xd@gmail.com>
Francisco Couzo <franciscouzo@gmail.com>
François Freitag <mail@franek.fr>

View File

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