[4.2.x] Fixed #34349 -- Fixed FormSet.empty_form crash when deleting extra forms is disabled.

Backport of 6cbc403b8e from main
This commit is contained in:
Laurens Verhoeven 2023-02-17 21:05:00 +01:00 committed by Mariusz Felisiak
parent aab25a69dd
commit bb94e1b7db
2 changed files with 4 additions and 1 deletions

View File

@ -492,7 +492,9 @@ class BaseFormSet(RenderableFormMixin):
required=False,
widget=self.get_ordering_widget(),
)
if self.can_delete and (self.can_delete_extra or index < initial_form_count):
if self.can_delete and (
self.can_delete_extra or (index is not None and index < initial_form_count)
):
form.fields[DELETION_FIELD_NAME] = BooleanField(
label=_("Delete"),
required=False,

View File

@ -1482,6 +1482,7 @@ class FormsFormsetTestCase(SimpleTestCase):
self.assertIn("DELETE", formset.forms[0].fields)
self.assertNotIn("DELETE", formset.forms[1].fields)
self.assertNotIn("DELETE", formset.forms[2].fields)
self.assertNotIn("DELETE", formset.empty_form.fields)
formset = ChoiceFormFormset(
data={