[Django] #12213: Initial data and addition queryset_filter for formset from inlineformset_factory

45 views
Skip to first unread message

Django

unread,
Nov 14, 2009, 12:41:20 PM11/14/09
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------+------------------------------------------------
Reporter: ramusus | Owner: nobody
Status: new | Milestone:
Component: Uncategorized | Version: 1.1
Keywords: | Stage: Unreviewed
Has_patch: 0 |
---------------------------+------------------------------------------------
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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12213>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 14, 2009, 12:45:37 PM11/14/09
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
------------------------------------+---------------------------------------
Reporter: ramusus | Owner: nobody
Status: new | Milestone:
Component: Uncategorized | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
------------------------------------+---------------------------------------
Changes (by ramusus):

* cc: ram...@gmail.com (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:1>

Django

unread,
Nov 14, 2009, 12:46:02 PM11/14/09
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
------------------------------------+---------------------------------------
Reporter: ramusus | Owner: nobody
Status: new | Milestone:
Component: Uncategorized | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
------------------------------------+---------------------------------------
Changes (by ramusus):

* has_patch: 0 => 1
* needs_tests: 0 => 1

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:2>

Django

unread,
Jan 10, 2010, 12:49:51 PM1/10/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: new | Milestone:
Component: Forms | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Changes (by lorochka85):

* component: Uncategorized => Forms

Comment:

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]

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:3>

Django

unread,
Feb 5, 2010, 9:50:46 AM2/5/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: closed | Milestone:
Component: Forms | Version: 1.1
Resolution: fixed | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Changes (by russellm):

* status: new => closed
* resolution: => fixed

Comment:

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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:4>

Django

unread,
Jul 3, 2010, 8:33:43 AM7/3/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: reopened | Milestone:
Component: Forms | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Changes (by sehma...@gmail.com):

* status: closed => reopened
* resolution: fixed =>

Comment:

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?

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:5>

Django

unread,
Jul 3, 2010, 8:34:18 AM7/3/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: reopened | Milestone:
Component: Forms | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Changes (by anonymous):

* cc: sehma...@gmail.com (added)

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:6>

Django

unread,
Jul 3, 2010, 9:22:55 AM7/3/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: reopened | Milestone:
Component: Forms | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Comment (by sehma...@gmail.com):

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

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:7>

Django

unread,
Jul 4, 2010, 8:01:38 AM7/4/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: reopened | Milestone:
Component: Forms | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Comment (by sehma...@gmail.com):

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.

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:8>

Django

unread,
Sep 11, 2010, 3:24:46 PM9/11/10
to djang...@holovaty.com, django-...@googlegroups.com
#12213: Initial data and addition queryset_filter for formset from
inlineformset_factory
---------------------------------+------------------------------------------
Reporter: ramusus | Owner: nobody
Status: closed | Milestone:
Component: Forms | Version: 1.1
Resolution: fixed | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------+------------------------------------------
Changes (by tobias):

* status: reopened => closed
* resolution: => fixed

Comment:

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

--
Ticket URL: <http://code.djangoproject.com/ticket/12213#comment:9>
Reply all
Reply to author
Forward
0 new messages