Re: [Django] #33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs

13 views
Skip to first unread message

Django

unread,
Sep 8, 2022, 4:17:31 AM9/8/22
to django-...@googlegroups.com
#33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs
-------------------------------------+-------------------------------------
Reporter: claypooj21 | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 4.1
Severity: Normal | Resolution:
Keywords: formset, | Triage Stage: Accepted
empty_form, empty_permitted, form |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

The `KeyError` is confusing here. It's raised because we're in the context
of rendering the template:


{{{
>>> self.form(empty_permitted=True, **self.get_form_kwargs(None))
Traceback (most recent call last):
File "/Users/carlton/Projects/Django/django/django/template/base.py",
line 880, in _resolve_lookup
current = current[bit]
File "/Users/carlton/Projects/Django/django/django/forms/formsets.py",
line 118, in __getitem__
return self.forms[index]
TypeError: list indices must be integers or slices, not str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<console>", line 1, in <module>
KeyError: 'empty_permitted'
}}}


The real exception is better seen using the formset directly:


{{{
>>> from ticket_33995.models import MyModel
>>> from django.forms import modelformset_factory
>>> ff = modelformset_factory(MyModel, fields=['name'])
>>> formset = ff(queryset=MyModel.objects.none(),
form_kwargs={'empty_permitted': True})
>>> formset.empty_form
>
/Users/carlton/Projects/Django/django/django/forms/formsets.py(262)empty_form()
-> form_kwargs = self.get_form_kwargs(None)
(Pdb) c
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/carlton/Projects/Django/django/django/forms/formsets.py",
line 265, in empty_form
form = self.form(
TypeError: django.forms.widgets.MyModelForm() got multiple values for
keyword argument 'empty_permitted'
}}}

That's expected:


{{{
>>> class Example:
... def __init__(self, a_kwarg=None):
... pass
...
>>> Example(a_kwarg=True)
<__main__.Example object at 0x102352950>
>>> Example(a_kwarg=True, a_kwarg=False)
File "<stdin>", line 1
SyntaxError: keyword argument repeated: a_kwarg
>>> {"a":1, **{"a":2}}
{'a': 2}
>>> Example(a_kwarg=True, **{"a_kwarg": False})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __main__.Example() got multiple values for keyword argument
'a_kwarg'
}}}

Resolving the `kwargs` before the constructor call, as per Mariusz'
suggestion would resolve.

#21501 was on a similar topic.

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

Django

unread,
Sep 9, 2022, 3:50:52 AM9/9/22
to django-...@googlegroups.com
#33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs
-------------------------------------+-------------------------------------
Reporter: claypooj21 | Owner: Bhuvnesh
Type: Bug | Status: assigned

Component: Forms | Version: 4.1
Severity: Normal | Resolution:
Keywords: formset, | Triage Stage: Accepted
empty_form, empty_permitted, form |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bhuvnesh):

* owner: nobody => Bhuvnesh
* status: new => assigned


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

Django

unread,
Sep 9, 2022, 6:36:23 AM9/9/22
to django-...@googlegroups.com
#33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs
-------------------------------------+-------------------------------------
Reporter: claypooj21 | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Forms | Version: 4.1
Severity: Normal | Resolution:
Keywords: formset, | Triage Stage: Accepted
empty_form, empty_permitted, form |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Sep 9, 2022, 7:53:49 AM9/9/22
to django-...@googlegroups.com
#33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs
-------------------------------------+-------------------------------------
Reporter: claypooj21 | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Forms | Version: 4.1
Severity: Normal | Resolution:
Keywords: formset, | Triage Stage: Ready for
empty_form, empty_permitted, 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):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
Sep 9, 2022, 8:19:27 AM9/9/22
to django-...@googlegroups.com
#33995: Rendering empty_form crashes when empty_permitted is passed to form_kwargs
-------------------------------------+-------------------------------------
Reporter: claypooj21 | Owner: Bhuvnesh
Type: Bug | Status: closed
Component: Forms | Version: 4.1
Severity: Normal | Resolution: fixed

Keywords: formset, | Triage Stage: Ready for
empty_form, empty_permitted, 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:"f3cd252cfc46c0c7d66e765818dd3dadf60d4d0e" f3cd252c]:
{{{
#!CommitTicketReference repository=""
revision="f3cd252cfc46c0c7d66e765818dd3dadf60d4d0e"
Fixed #33995 -- Fixed FormSet.empty_form crash when empty_permitted is
passed to form_kwargs.
}}}

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

Reply all
Reply to author
Forward
0 new messages