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.
* owner: nobody => Bhuvnesh
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33995#comment:3>
* 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>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33995#comment:5>
* 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>