Thursday, September 29, 2016

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

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.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/16cf8cc0-de4a-47e5-99bc-66322d0d3066%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment