diff --git a/AUTHORS b/AUTHORS index a8b7fecf87df..a3848545e64d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -330,6 +330,7 @@ answer newbie questions, and generally made Django that much better: Florian Demmer Florian Moussous Fran Hrženjak + Francesco Panico Francisco Albarran Cristobal Francisco Couzo François Freitag diff --git a/django/forms/forms.py b/django/forms/forms.py index 7c9f1034d2ea..6884e6e724d2 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -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: @@ -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): diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 600547219436..930a200b14a3 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -4579,6 +4579,22 @@ def test_label_attrs_not_localized(self): 'First name:', ) + 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):