[Django] #34349: Formsets' add_fields() method fails in some circumstances if the argument index is None.

0 views
Skip to first unread message

Django

unread,
Feb 17, 2023, 2:39:08 PM2/17/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
laurensverhoeven |
Type: Bug | Status: new
Component: Forms | Version: dev
Severity: Normal | Keywords: formset
Triage Stage: | can_delete_extra empty_form
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Formsets' `add_fields()` method fails in some circumstances if the
argument `index` is `None`.

When a FormSet has the attributes `self.can_delete == True` and
`self.can_delete_extra == False`, calling the `add_fields()` method on
that FormSet fails if the argument `index` is `None`. This occurs for
example when calling `FormSet.empty_form()`. The result is that the method
raises the exception `TypeError: '<' not supported between instances of
'NoneType' and 'int'`.

Code example:
{{{#!python
MyFormSet = forms.formset_factory(
form=MyForm,
can_delete=True,
can_delete_extra=False,
)
my_formset = MyFormSet(
initial=None,
)
print(my_formset.empty_form)
}}}

The reason this happens is that in in line 493 of
[django.forms.formsets](https://github.com/django/django/blob/main/django/forms/formsets.py)
`index` is compared to `initial_form_count`:
{{{#!python
if self.can_delete and (self.can_delete_extra or index <
initial_form_count):
}}}

Checking for index `not None` should fix the issue:
{{{#!python
if self.can_delete and (self.can_delete_extra or (index is not None and
index < initial_form_count)):
}}}

== How to Reproduce

A self-contained example to reproduce this bug is as follows:

{{{#!python
#!/usr/bin/env python3

import os

import django
from django import forms


class MyForm(forms.Form):
my_field = forms.CharField()


if __name__ == "__main__":
settings_file = os.path.splitext(os.path.basename(__file__))[0]
django.conf.settings.configure(
DEBUG=True,
MIDDLEWARE_CLASSES=[],
ROOT_URLCONF=settings_file,
)
django.setup()

MyFormSet = forms.formset_factory(
form=MyForm,
can_delete=True,
can_delete_extra=False,
)
my_formset = MyFormSet(
initial=None,
)

print(my_formset.empty_form)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34349>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 17, 2023, 2:42:58 PM2/17/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: Laurens Verhoeven | Owner: Laurens
| Verhoeven
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:

Keywords: formset | Triage Stage:
can_delete_extra empty_form | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Laurens Verhoeven):

* owner: nobody => Laurens Verhoeven
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:1>

Django

unread,
Feb 17, 2023, 2:46:11 PM2/17/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: laurensverhoeven | Owner:
| laurensverhoeven
Type: Bug | Status: assigned
Component: Forms | Version: 4.2

Severity: Normal | Resolution:
Keywords: formset | Triage Stage:
can_delete_extra empty_form | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by laurensverhoeven):

* version: dev => 4.2


--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:2>

Django

unread,
Feb 17, 2023, 3:13:41 PM2/17/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: laurensverhoeven | Owner:
| laurensverhoeven
Type: Bug | Status: assigned
Component: Forms | Version: dev

Severity: Normal | Resolution:
Keywords: formset | Triage Stage:
can_delete_extra empty_form | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by laurensverhoeven):

* version: 4.2 => dev


--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:3>

Django

unread,
Feb 17, 2023, 3:32:16 PM2/17/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: Laurens Verhoeven | Owner: Laurens
| Verhoeven
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: formset | Triage Stage: Accepted
can_delete_extra empty_form |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

[https://github.com/django/django/pull/16569 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:4>

Django

unread,
Feb 20, 2023, 3:02:02 AM2/20/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: Laurens Verhoeven | Owner: Laurens
| Verhoeven
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: formset | Triage Stage: Ready for
can_delete_extra empty_form | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:5>

Django

unread,
Feb 20, 2023, 3:32:18 AM2/20/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: Laurens Verhoeven | Owner: Laurens
| Verhoeven
Type: Bug | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: fixed

Keywords: formset | Triage Stage: Ready for
can_delete_extra empty_form | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"6cbc403b8ee7014bd6dae4892d404eedb1d4a50d" 6cbc403]:
{{{
#!CommitTicketReference repository=""
revision="6cbc403b8ee7014bd6dae4892d404eedb1d4a50d"
Fixed #34349 -- Fixed FormSet.empty_form crash when deleting extra forms
is disabled.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:6>

Django

unread,
Feb 20, 2023, 3:33:46 AM2/20/23
to django-...@googlegroups.com
#34349: Formsets' add_fields() method fails in some circumstances if the argument
index is None.
-------------------------------------+-------------------------------------
Reporter: Laurens Verhoeven | Owner: Laurens
| Verhoeven
Type: Bug | Status: closed
Component: Forms | Version: dev
Severity: Normal | Resolution: fixed
Keywords: formset | Triage Stage: Ready for
can_delete_extra empty_form | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"bb94e1b7dbd42af51d805efafe52b22eb86d5ebe" bb94e1b7]:
{{{
#!CommitTicketReference repository=""
revision="bb94e1b7dbd42af51d805efafe52b22eb86d5ebe"
[4.2.x] Fixed #34349 -- Fixed FormSet.empty_form crash when deleting extra
forms is disabled.

Backport of 6cbc403b8ee7014bd6dae4892d404eedb1d4a50d from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34349#comment:7>

Reply all
Reply to author
Forward
0 new messages