Skip to content

Commit

Permalink
[4.2.x] Fixed #34349 -- Fixed FormSet.empty_form crash when deleting …
Browse files Browse the repository at this point in the history
…extra forms is disabled.

Backport of 6cbc403 from main
  • Loading branch information
laurensverhoeven authored and felixxm committed Feb 20, 2023
1 parent aab25a6 commit bb94e1b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django/forms/formsets.py
Expand Up @@ -492,7 +492,9 @@ def add_fields(self, form, index):
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,
Expand Down
1 change: 1 addition & 0 deletions tests/forms_tests/tests/test_formsets.py
Expand Up @@ -1482,6 +1482,7 @@ def test_disable_delete_extra_formset_forms(self):
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={
Expand Down

0 comments on commit bb94e1b

Please sign in to comment.