[Django] #18166: Allow the formset class to contribute kwargs to form instances

7 views
Skip to first unread message

Django

unread,
Apr 18, 2012, 9:39:01 AM4/18/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-----------------------------+----------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Keywords: forms formset kwargs
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------+----------------------------------
When we use a formset, each form is instantiated by the method
BaseFormSet._construct_forms(), which is called from
BaseFormSet.__init__()

_construct_forms() allows for additional kwargs to be passed through to
the individual form constructor, but BaseFormSet.__init__() does not
provide any mechanism for kwargs to be passed through to the
_construct_forms() call.

Also, we may not be able to supply a static unchanging list of kwargs that
should be supplied to each form constructor - the kwargs we want to pass
may depend upon the specific instance of the form being instantiated.

I propose the following changes:

Add a form_kwargs argument to BaseFormSet.__init__(). This would be stored
on self, and consumed in BaseFormSet._construct_forms()

Add a method, BaseFormSet._kwargs_for_form(self, i). This method would be
called for each form instantiated, passing the index of the form within
the formset. The return value of this method would be merged with the
form_kwargs supplied in BaseFormSet.__init__() and used to instantiate the
form.

With these changes, a user could add additional constructor arguments to
all forms by passing in form_kwargs to the formset constructor, and could
add additional constructor arguments to specific forms by implementing
_kwargs_for_form on a derived BaseFormSet class.

(I found it strange, when BaseFormSet was written, it was clearly intended
to be able to pass kwargs to the forms - _construct_form() allows for it,
there just is not the plumbing to do so. I worked around this by
redefining - copy/pasting - the _construct_forms() method in my derived
BaseFormSet class)

Patch doesn't have tests or doc changes, will add if this idea gets
traction.

Cheers

Tom

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

Django

unread,
Apr 18, 2012, 9:42:59 AM4/18/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage:
kwargs | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by tevans):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Hmm, wiki markup that turns !__init!__ into __init__ is less than useful
for Python discussion :/

--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:1>

Django

unread,
Apr 18, 2012, 10:21:37 AM4/18/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage:
kwargs | Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by tevans):

* needs_docs: 0 => 1
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:2>

Django

unread,
Apr 24, 2012, 10:05:44 AM4/24/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage:
kwargs | Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Old description:
New description:

When we use a formset, each form is instantiated by the method
{{{BaseFormSet._construct_forms()}}}, which is called from
{{{BaseFormSet.__init__()}}}

{{{_construct_forms()}}} allows for additional kwargs to be passed through
to the individual form constructor, but {{{BaseFormSet.__init__()}}} does
not provide any mechanism for kwargs to be passed through to the
{{{_construct_forms()}}} call.

Also, we may not be able to supply a static unchanging list of kwargs that
should be supplied to each form constructor - the kwargs we want to pass
may depend upon the specific instance of the form being instantiated.

I propose the following changes:

Add a {{{form_kwargs}}} argument to {{{BaseFormSet.__init__()}}}. This
would be stored on self, and consumed in
{{{BaseFormSet._construct_forms()}}}

Add a method, {{{BaseFormSet._kwargs_for_form(self, i)}}}. This method
would be called for each form instantiated, passing the index of the form
within the formset. The return value of this method would be merged with
the form_kwargs supplied in {{{BaseFormSet.__init__()}}} and used to
instantiate the form.

With these changes, a user could add additional constructor arguments to
all forms by passing in form_kwargs to the formset constructor, and could
add additional constructor arguments to specific forms by implementing
_kwargs_for_form on a derived BaseFormSet class.

(I found it strange, when {{{BaseFormSet}}} was written, it was clearly
intended to be able to pass kwargs to the forms - {{{_construct_form()}}}
allows for it, there just is not the plumbing to do so. I worked around
this by redefining - copy/pasting - the {{{_construct_forms()}}} method in
my derived {{{BaseFormSet}}} class)

Patch doesn't have tests or doc changes, will add if this idea gets
traction.

Cheers

Tom

--

Comment (by lrekucki):

Replying to [comment:1 tevans]:

> Hmm, wiki markup that turns !__init!__ into __init__ is less than useful
for Python discussion :/

You can use formatting: !{{{ __init__ }}} will show as {{{__init__}}}.
There's both live preview and a preview button.

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

Django

unread,
Apr 24, 2012, 10:24:58 AM4/24/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage:
kwargs | Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by akaariai):

Just as an idea: how about defining a new method:
{{{
def get_single_form(self, i, defaults):
"""
Override me to have total control over the single forms.
"""
return self.form(**defaults)
}}}
and use that instead of self.form() in the _construct_form() method. This
would be more powerful and easier to implement.

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

Django

unread,
Apr 27, 2012, 5:14:52 AM4/27/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: SVN
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage:
kwargs | Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by tevans):

Replying to [comment:4 akaariai]:

> Just as an idea: how about defining a new method:
> {{{
> def get_single_form(self, i, defaults):
> """
> Override me to have total control over the single forms.
> """
> return self.form(**defaults)
> }}}
> and use that instead of self.form() in the _construct_form() method.
This would be more powerful and easier to implement.

Yes, I like that. I would like to also keep the idea of being able to
contribute to a form's kwargs without defining a {{{BaseFormSet}}}
subclass, eg by passing them to {{{BaseFormSet.__init__}}}.

I should get some time this w/e to re-roll the patch.

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

Django

unread,
May 20, 2012, 1:32:07 PM5/20/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
--------------------------------------+------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: forms formset kwargs | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by anonymous):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Agreed, a method for implementing this is a good idea, but please let's
skip the redundant 'simple' prefix, just call it `get_form`.

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

Django

unread,
May 20, 2012, 1:32:28 PM5/20/12
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
--------------------------------------+------------------------------------
Reporter: tevans | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: forms formset kwargs | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by jezdez):

Oops that was me.

--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:7>

Django

unread,
Jun 4, 2015, 6:08:46 AM6/4/15
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: sergei-
| maertens
Type: New feature | Status: assigned

Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage: Accepted
kwargs |

Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by sergei-maertens):

* owner: nobody => sergei-maertens
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:8>

Django

unread,
Jun 4, 2015, 6:08:57 AM6/4/15
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: sergei-
| maertens
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage: Accepted
kwargs |
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by sergei-maertens):

I'll have a go at this

--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:9>

Django

unread,
Jun 4, 2015, 9:22:55 AM6/4/15
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: sergei-
| maertens
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: forms formset | Triage Stage: Accepted
kwargs |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by sergei-maertens):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:10>

Django

unread,
Jun 5, 2015, 6:10:50 AM6/5/15
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: sergei-
| maertens
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: forms formset | Triage Stage: Accepted
kwargs |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Russell Keith-Magee <russell@…>):

In [changeset:"fe21fb810a1bd12b10c534923809423b5c1cf4d7" fe21fb81]:
{{{
#!CommitTicketReference repository=""
revision="fe21fb810a1bd12b10c534923809423b5c1cf4d7"
Merge pull request #4757 from sergei-maertens/ticket_18166

Fixed #18166 -- Added ability to pass kwargs to the form constructor in a
formset.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:12>

Django

unread,
Jun 5, 2015, 6:10:50 AM6/5/15
to django-...@googlegroups.com
#18166: Allow the formset class to contribute kwargs to form instances
-------------------------------------+-------------------------------------
Reporter: tevans | Owner: sergei-
| maertens
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: forms formset | Triage Stage: Accepted
kwargs |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergei Maertens <sergei@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"238e2ac3690755d0e6c2dfca815e2b6f84a47f6e" 238e2ac3]:
{{{
#!CommitTicketReference repository=""
revision="238e2ac3690755d0e6c2dfca815e2b6f84a47f6e"
Fixed #18166 -- Added form_kwargs support to formsets.

By specifying form_kwargs when instantiating the formset, or overriding
the `get_form_kwargs` method on a formset class, you can pass extra
keyword arguments to the underlying `Form` instances.

Includes tests and documentation update.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/18166#comment:11>

Reply all
Reply to author
Forward
0 new messages