From 51faf4bd172cd4cb219a9793facbfa00246c9f3c Mon Sep 17 00:00:00 2001 From: Francesco Panico Date: Thu, 17 Nov 2022 22:45:06 +0100 Subject: [PATCH] Fixed #34148 -- Reverted "Fixed #32901 -- Optimized BaseForm.__getitem__()." This reverts commit edde2a069929c93e37835dc3f7c9a229040058e2. Thanks Jan Pieter Waagmeester for the report. --- AUTHORS | 1 + django/forms/forms.py | 10 +++------- tests/forms_tests/tests/test_forms.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/AUTHORS b/AUTHORS index a8b7fecf87..a3848545e6 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 7c9f1034d2..6884e6e724 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -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): diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 6005472194..930a200b14 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -4579,6 +4579,22 @@ Options: