From f60d5e46e131b94d6ecc92b6891689ccc94bd1c9 Mon Sep 17 00:00:00 2001 From: antoliny0919 Date: Sun, 17 Nov 2024 11:25:44 +0900 Subject: [PATCH] Fixed #35913 -- Prevented formset name suffix 'FormFormSet'. --- django/forms/formsets.py | 7 ++++++- tests/forms_tests/tests/test_formsets.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/forms/formsets.py b/django/forms/formsets.py index c8e5893f19..c2663154d4 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -570,7 +570,12 @@ def formset_factory( "validate_max": validate_max, "renderer": renderer, } - return type(form.__name__ + "FormSet", (formset,), attrs) + form_name = form.__name__ + if form_name.endswith("Form"): + formset_name = form_name + "Set" + else: + formset_name = form_name + "FormSet" + return type(formset_name, (formset,), attrs) def all_valid(formsets): diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index f80c1dc09e..9f7012a11f 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -149,6 +149,12 @@ class FormsFormsetTestCase(SimpleTestCase): self.assertFalse(formset.is_valid()) self.assertFalse(formset.has_changed()) + def test_formset_name(self): + ArticleFormSet = formset_factory(ArticleForm) + ChoiceFormSet = formset_factory(Choice) + self.assertEqual(ArticleFormSet.__name__, "ArticleFormSet") + self.assertEqual(ChoiceFormSet.__name__, "ChoiceFormSet") + def test_form_kwargs_formset(self): """ Custom kwargs set on the formset instance are passed to the