Skip to content

Commit

Permalink
Fixed #34349 -- Fixed FormSet.empty_form crash when deleting extra fo…
Browse files Browse the repository at this point in the history
…rms is disabled.
  • Loading branch information
laurensverhoeven authored and felixxm committed Feb 20, 2023
1 parent 278881e commit 6cbc403
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
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,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
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,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 6cbc403

Please sign in to comment.