Skip to content

Commit

Permalink
Fixed #34148 -- Reverted "Fixed #32901 -- Optimized BaseForm.__getite…
Browse files Browse the repository at this point in the history
…m__()."

This reverts commit edde2a0.

Thanks Jan Pieter Waagmeester for the report.
  • Loading branch information
panicofr authored and felixxm committed Nov 18, 2022
1 parent fbde929 commit 51faf4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ answer newbie questions, and generally made Django that much better:
Florian Demmer <[email protected]>
Florian Moussous <[email protected]>
Fran Hrženjak <[email protected]>
Francesco Panico <[email protected]>
Francisco Albarran Cristobal <[email protected]>
Francisco Couzo <[email protected]>
François Freitag <[email protected]>
Expand Down
10 changes: 3 additions & 7 deletions django/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ def __iter__(self):

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:
Expand All @@ -189,9 +185,9 @@ def __getitem__(self, name):
", ".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):
Expand Down
16 changes: 16 additions & 0 deletions tests/forms_tests/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4579,6 +4579,22 @@ def test_label_attrs_not_localized(self):
'<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):
Expand Down

0 comments on commit 51faf4b

Please sign in to comment.