Formset: Is get_form_kwargs() has a bad example in documentation?

92 views
Skip to first unread message

Nonverbis M

unread,
Sep 29, 2016, 9:13:45 AM9/29/16
to Django users
https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#passing-custom-parameters-to-formset-forms

In the documentation we can read: 

====Quotation=====

The form_kwargs may also depend on the specific form instance. The formset base class provides aget_form_kwargs method. The method takes a single argument - the index of the form in the formset. The index is None for the empty_form:

>>> from django.forms import BaseFormSet
>>> from django.forms import formset_factory

>>> class BaseArticleFormSet(BaseFormSet):
...     def get_form_kwargs(self, index):
...         kwargs = super(BaseArticleFormSet, self).get_form_kwargs(index)
...         kwargs['custom_kwarg'] = index
...         return kwargs
====Quotation=====
I can see that 
1) Example doesn't illustrate the text of documentation. It illustrates redefinition of the method.
2) The example seems to be bad itself. Doesn't this brings about some side effect? 
https://en.wikipedia.org/wiki/Side_effect_(computer_science)

Wouldn't just kwargs = formset.get_form_kwargs(0) be better?
If this makes sense, I could create a ticket at Djangoproject. 
But I'm too green for deep understanding this example. So, I decided to discuss it here.



Reply all
Reply to author
Forward
0 new messages