initial data for inlineformset_factory

1,802 views
Skip to first unread message

patrickk

unread,
Aug 19, 2008, 2:10:40 PM8/19/08
to Django developers
any reason, why it´s currently impossible to use "initial" with
inlineformset_factory. is it planned to integreate this feature?

thanks,
patrick

Brian Rosner

unread,
Aug 19, 2008, 3:22:58 PM8/19/08
to django-d...@googlegroups.com
On Tue, Aug 19, 2008 at 12:10 PM, patrickk <pat...@vonautomatisch.at> wrote:
>
> any reason, why it´s currently impossible to use "initial" with
> inlineformset_factory. is it planned to integreate this feature?

I guess I am failing to see how this is useful? initial data in an
inline model formset is based on the queryset such as it is a subclass
of model formset. Could you provide a use case where it might be worth
it to also provide initial data for an inline model formset? It would
seem that a default value on a model field would be sufficient. Seems
a bit overkill for the general case due to the unknown size of the
queryset.

--
Brian Rosner
http://oebfare.com

patrickk

unread,
Aug 19, 2008, 3:49:37 PM8/19/08
to Django developers
usecase:
let´s say you have a "booking" (form) with a number of X "booking
amounts" (formset). now the user could be able to save a pre-defined
template for common bookings. these templates are saved as "template"
and "template amounts". so, if the user selects a given template, the
template-amounts should be the "initial date" for the booking amounts.

maybe this is totally out of scope, I´m not sure. and maybe there´s
another way to insert the template ... it just seemed very natural for
me to use "initial" with an inline mode formset, till I figured out it
´s not possible.

patrick.


On Aug 19, 9:22 pm, "Brian Rosner" <bros...@gmail.com> wrote:

Justin Fagnani

unread,
Aug 19, 2008, 3:54:18 PM8/19/08
to django-d...@googlegroups.com
On Tue, Aug 19, 2008 at 12:22 PM, Brian Rosner <bro...@gmail.com> wrote:
>Seems
> a bit overkill for the general case due to the unknown size of the
> queryset.

It is possible to specify both a queryset and initial data with a
ModelFormSet or InlineModelFormSet, and initial will override the
queryset, but the queryset is still kept around. Looks like that could
cause some problems in save_existing_objects(), especially if initial
and queryset aren't the same size.

-Justin

Justin Fagnani

unread,
Aug 19, 2008, 4:05:40 PM8/19/08
to django-d...@googlegroups.com
Uh.. disregard that. My mistake. I read the code wrong and
BaseInlineFormSet does not take **kwargs and pass them to
BaseModelFormSet.__init__(), so no you can't specify 'initial'. Which
is good.

-Justin

patrickk

unread,
Aug 19, 2008, 4:57:27 PM8/19/08
to Django developers
now this question is probably better posted in django-users, but:
is there any way to prepopulate an unbound formset using
inlineformset_factory?

thanks,
patrick

On Aug 19, 10:05 pm, "Justin Fagnani" <justin.fagn...@gmail.com>
wrote:
> Uh.. disregard that. My mistake. I read the code wrong and
> BaseInlineFormSet does not take **kwargs and pass them to
> BaseModelFormSet.__init__(), so no you can't specify 'initial'. Which
> is good.
>
> -Justin
>
> On Tue, Aug 19, 2008 at 12:54 PM, Justin Fagnani
>
> <justin.fagn...@gmail.com> wrote:

nathan

unread,
Sep 2, 2008, 8:15:35 PM9/2/08
to Django developers
On Aug 19, 2:22 pm, "Brian Rosner" <bros...@gmail.com> wrote:

> I guess I am failing to see how this is useful?initialdatain an
> inline model formset is based on the queryset such as it is a subclass
> of model formset. Could you provide a use case where it might be worth
> it to also provideinitialdatafor an inline model formset? It would
> seem that a default value on a model field would be sufficient. Seems
> a bit overkill for the general case due to the unknown size of the
> queryset.
>
> --
> Brian Rosnerhttp://oebfare.com

I guess my question would be, how can it not be useful? If it was
considered worthwhile for a regular formset to be able to have dynamic
field values via the initial argument why wouldn't it be useful for a
model formset? Whether or not the data is saved in a database seems
irrelevant to the usefulness of being able to provide dynamic initial
values.

You seem to be thinking of initial data in terms of overriding
existing instances but where it comes in handy is for providing
defaults for potential new instances. As a use case consider a
restaurant. When a customer wants to buy something they create an
order and add one or more menu items to it, say 2 hot dogs, 4
hamburgers, and 1 large drink.

It would be nice the next time the customer comes back to provide
those as the defaults for a new order. In that way they could just
get the usual. Or if the menu were small it might be best to always
display all menu items and just have customers enter the quantity for
anything they want. In this case you would override the validation
and save methods so that only records with a quantity provided were
saved into your database, similar to the way the core option used to
work for model fields.

I really can't think of a common use case where initial would be used
to override existing instances. However, I think it would be helpful
if a model formset displayed existing instances supplied by the
underlying queryset, new instances populated with data from an initial
argument, and blank instances defined by extra. Extra already
operates as I would expect initial to, but it takes its values from
the fields rather than allowing you to pass them in dynamically. If
extra is worthwhile to have it seems initial would be too.
Reply all
Reply to author
Forward
0 new messages