From f3cd252cfc46c0c7d66e765818dd3dadf60d4d0e Mon Sep 17 00:00:00 2001 From: DevilsAutumn Date: Fri, 9 Sep 2022 15:21:41 +0530 Subject: [PATCH] Fixed #33995 -- Fixed FormSet.empty_form crash when empty_permitted is passed to form_kwargs. --- django/forms/formsets.py | 15 ++++++++------- tests/forms_tests/tests/test_formsets.py | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 57676428ff..f14e1aae83 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -257,14 +257,15 @@ class BaseFormSet(RenderableFormMixin): @property def empty_form(self): - form = self.form( - auto_id=self.auto_id, - prefix=self.add_prefix("__prefix__"), - empty_permitted=True, - use_required_attribute=False, + form_kwargs = { **self.get_form_kwargs(None), - renderer=self.renderer, - ) + "auto_id": self.auto_id, + "prefix": self.add_prefix("__prefix__"), + "empty_permitted": True, + "use_required_attribute": False, + "renderer": self.renderer, + } + form = self.form(**form_kwargs) self.add_fields(form, None) return form diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index c36a79d4e0..f0e4b154b7 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -179,6 +179,10 @@ class FormsFormsetTestCase(SimpleTestCase): self.assertTrue(hasattr(formset.empty_form, "custom_kwarg")) self.assertEqual(formset.empty_form.custom_kwarg, 1) + def test_empty_permitted_ignored_empty_form(self): + formset = ArticleFormSet(form_kwargs={"empty_permitted": False}) + self.assertIs(formset.empty_form.empty_permitted, True) + def test_formset_validation(self): # FormSet instances can also have an error attribute if validation failed for # any of the forms.