#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701831c2ce30c-281d6634-d7f4-4d14-b380-e9c48ced8f2f-000000%40eu-central-1.amazonses.com.