From edde2a069929c93e37835dc3f7c9a229040058e2 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Mon, 5 Jul 2021 06:21:23 -0700 Subject: [PATCH] Fixed #32901 -- Optimized BaseForm.__getitem__(). --- django/forms/forms.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/django/forms/forms.py b/django/forms/forms.py index 14f5dea4cd..42ef772655 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -149,6 +149,10 @@ class BaseForm: 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: @@ -159,9 +163,9 @@ class BaseForm: ', '.join(sorted(self.fields)), ) ) - 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] + bound_field = field.get_bound_field(self, name) + self._bound_fields_cache[name] = bound_field + return bound_field @property def errors(self):