Opened 7 years ago

Closed 6 years ago

Last modified 5 years ago

#12213 closed Uncategorized (fixed)

Initial data and addition queryset_filter for formset from inlineformset_factory

Reported by: ramusus Owned by: nobody
Component: Forms Version: 1.1
Severity: Normal Keywords:
Cc: ramusus@…, sehmaschine@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


To the continue of the discussion here:

I also need the ability to define initial data for formset from inlineformset_factory. It's very strange to limit this by the framework for inlineformset_factory, but not limit for modelformset_factory.

Queryset parameter created automatically inside inlineformset_factory, but I can imagine situation, when it's necessary to generate form and formset with not all linked objects. For example If I need to see formset with instances, filtered by special condition, based on fields. For this purpose I added queryset_filter parameter, that passed as a kwargs argument into queryset.filter(queryset_filter).

Formsets is a great mechanism, that simplify many routine operations with linked objects. I think it's better to have ability tune this mecanism in detail.

Attachments (1)

12213.diff (1.2 KB) - added by ramusus 7 years ago.
Patch with fixing this gaps

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by ramusus

Attachment: 12213.diff added

Patch with fixing this gaps

comment:1 Changed 7 years ago by ramusus

Cc: ramusus@… added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 7 years ago by ramusus

Has patch: set
Needs tests: set

comment:3 Changed 7 years ago by lorochka85

Component: UncategorizedForms

Queryset support was added to BaseInlineFormSet (not initial though) in the development version and, imho, is better than dealing with filters. For initial, adding it as an extra argument to BaseInlineFormset and then passing to the super's __init__ does the job.

However, it gets a little bit confusing when one tries to pass both initial and queryset to ether BaseInlineFormSet or BaseModelFormSet. For example, you have something like:

formset = BaseModelFormSet(...,
                           initial=[{'field1': 'f1value1', 'field2': 'f2value1'},
                                    {'field1': 'f1value2', 'field2': 'f2value2'}],

then values for field1 and field2 for the first 2 items in some_qs will get overriden with values in initial. In order for the formset to display first data from the queryset and then extra forms with values from initial, your initial should look something like:

initial = [{} for i in range(0, some_qs.count())] +
          [{'field1': 'f1value1', 'field2': 'f2value1'},
           {'field1': 'f1value2', 'field2': 'f2value2'}]

...Having said all that, there is a nice little comment about instance and initial in BaseModelForm.__init__ :

# if initial was provided, it should override the values from instance

I suppose this is valid for formsets as well. Should that be documented somewhere?

Now, in terms of use cases for initial on InlineFormSet, there is one by Nathan in this thread:

comment:4 Changed 7 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

I think this may have been resolved by some of the changes introduced to support multiple databases in the admin. The approach is slightly different - it allows outright specification of a queryset, rather than a queryset filter and an initial - but I think it acheives the same goal. Reopen if I've missed something.

comment:5 Changed 6 years ago by sehmaschine@…

Resolution: fixed
Status: closedreopened

reopening this for the following reasons:
– no docs. how is queryset supposed to work like initial-data? any examples? I´ve tried this for days without any luck.
– why use queryset in the first place when using initial-data is more coherent?

comment:6 Changed 6 years ago by anonymous

Cc: sehmaschine@… added

comment:7 Changed 6 years ago by sehmaschine@…

btw: the above patch works great. any reason for not adding this patch?

comment:8 Changed 6 years ago by sehmaschine@…

after trying different setups I have to admit that the patch doesn´t work. unbelievable (and frustrating) that initial doesn´t work with inlineformset_factory. workaround for the example mentioned by nathan goes from a oneliner to about 50 lines of very nasty code.

comment:9 Changed 6 years ago by Tobias McNulty

Resolution: fixed
Status: reopenedclosed

The new way to pass the queryset that Russell mentions is documented here:

comment:10 Changed 5 years ago by anonymous

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

changing the queryset is NOT the same as using initials.
the "fix" mentioned in the last posting is not a fix to the problem.

comment:11 Changed 5 years ago by Claude Paroz

Initial data for extra forms of a model (inline) formset is discussed in #14574.

Note: See TracTickets for help on using tickets.
Back to Top