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.