#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)
Change History (12)
by , 15 years ago
Attachment: | 12213.diff added |
---|
comment:1 by , 15 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
Has patch: | set |
---|---|
Needs tests: | set |
comment:3 by , 15 years ago
Component: | Uncategorized → Forms |
---|
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 , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 , 14 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
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 , 14 years ago
Cc: | added |
---|
comment:8 by , 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 , 14 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
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 , 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 , 13 years ago
Initial data for extra forms of a model (inline) formset is discussed in #14574.
Patch with fixing this gaps