Opened 15 years ago

Closed 14 years ago

Last modified 13 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

Description

To the continue of the discussion here: http://groups.google.ru/group/django-developers/browse_thread/thread/73af9e58bd7626a8/e307d4865759a26e

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 15 years ago.
Patch with fixing this gaps

Download all attachments as: .zip

Change History (12)

by ramusus, 15 years ago

Attachment: 12213.diff added

Patch with fixing this gaps

comment:1 by ramusus, 15 years ago

Cc: ramusus@… added

comment:2 by ramusus, 15 years ago

Has patch: set
Needs tests: set

comment:3 by lorochka85, 15 years ago

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(...,
                           queryset=some_qs,
                           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__ :
http://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L228

# 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: http://groups.google.ru/group/django-developers/browse_thread/thread/73af9e58bd7626a8/e307d4865759a26e

comment:4 by Russell Keith-Magee, 15 years ago

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 by sehmaschine@…, 14 years ago

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 by anonymous, 14 years ago

Cc: sehmaschine@… added

comment:7 by sehmaschine@…, 14 years ago

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

comment:8 by sehmaschine@…, 14 years ago

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 by Tobias McNulty, 14 years ago

Resolution: fixed
Status: reopenedclosed

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

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset

comment:10 by anonymous, 13 years ago

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 by Claude Paroz, 13 years ago

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