[Django] #21501: BaseFormSet's empty_form isn't as extendable as _construct_form()

18 views
Skip to first unread message

Django

unread,
Nov 23, 2013, 12:33:54 PM11/23/13
to django-...@googlegroups.com
#21501: BaseFormSet's empty_form isn't as extendable as _construct_form()
-----------------------------------------+--------------------
Reporter: Keryn Knight <django@…> | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------------+--------------------
It is possible to wedge extra kwargs into each form in a Formset by doing
something like:
{{{
class MyFormSet(BaseFormSet):
def _construct_form(self, i, **kwargs):
kwargs['extrafield'] = 1
return super(MyFormSet, self)._construct_form(i, **kwargs)
}}}
which is useful for forms wanting to require additional arguments - for
inlines wanting to act on values in an existing parent instance, for
example.

However, the kwarg can't be required, because of the empty_form property,
which doesn't have any way of marshall extra kwargs to the form (though it
can add fields), as such, the empty_form definition must be copy-pasted
entirely, which isn't ideal if the definition upstream (ie: in Django)
shifts over time.

A private api method:
{{{
def _get_empty_form_kwargs(self):
return {}
}}}

which could be dictionary expanded into the empty_form would allow for
passing required arguments in [I believe] a backwards compatible way,
while allowing userland code to replace less of the core FormSet
functionality.

The final empty_form might be something like:
{{{
@property
def empty_form(self):
form = self.form(
auto_id=self.auto_id,
prefix=self.add_prefix('__prefix__'),
empty_permitted=True,
**self._get_empty_form_kwargs(),
)
self.add_fields(form, None)
return form
}}}

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

Django

unread,
Dec 5, 2013, 8:47:45 AM12/5/13
to django-...@googlegroups.com
#21501: BaseFormSet's empty_form isn't as extendable as _construct_form()
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: new
Type: New feature | Version: master
Component: Forms | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Feb 14, 2016, 12:46:05 PM2/14/16
to django-...@googlegroups.com
#21501: BaseFormSet's empty_form isn't as extendable as _construct_form()
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> |
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* cc: berker.peksag@… (added)
* resolution: => duplicate


Comment:

This looks like a duplicate of #18166. A new `get_form_kwargs` method has
been added in 238e2ac3690755d0e6c2dfca815e2b6f84a47f6e.

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

Reply all
Reply to author
Forward
0 new messages