1. load form
1. edit eligible related objects in the db
1. refresh form (without restarting the server)
1. observe field choices have changed
[ticket: 22841] also supports this theory, (although the rather old
[ticket: 18272] suggests otherwise).
The syntax for a field of this type is:
{{{#!python
foo = forms.ModelChoiceField(
queryset=Bar.objects.filter(baz=True)
)
}}}
Given that this looks very like a runtime-evaluated queryset, I suggest a
docs amendment that clarifies the behaviour. (I am not confident in making
that docs amendment myself, at least until I have properly understood the
intended behaviour.)
--
Ticket URL: <https://code.djangoproject.com/ticket/27501>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Old description:
> The
> [https://github.com/django/django/blob/master/django/forms/models.py#L1197
> comments] in `ModelChoiceField._get_choices` suggest that the `queryset`
> attribute is evaluated afresh every time the form is initialised, and
> testing confirms this:
>
> 1. load form
> 1. edit eligible related objects in the db
> 1. refresh form (without restarting the server)
> 1. observe field choices have changed
>
> [ticket: 22841] also supports this theory, (although the rather old
> [ticket: 18272] suggests otherwise).
>
> The syntax for a field of this type is:
>
> {{{#!python
> foo = forms.ModelChoiceField(
> queryset=Bar.objects.filter(baz=True)
> )
> }}}
>
> Given that this looks very like a runtime-evaluated queryset, I suggest a
> docs amendment that clarifies the behaviour. (I am not confident in
> making that docs amendment myself, at least until I have properly
> understood the intended behaviour.)
New description:
The
[https://github.com/django/django/blob/master/django/forms/models.py#L1197
comments] in `ModelChoiceField._get_choices` suggest that the `queryset`
attribute is evaluated afresh every time the form is initialised, and
testing confirms this:
1. load form
1. edit eligible related objects in the db
1. refresh form (without restarting the server)
1. observe field choices have changed
#22841 also supports this theory, (although the rather old #18272 suggests
otherwise).
The syntax for a field of this type is:
{{{#!python
foo = forms.ModelChoiceField(
queryset=Bar.objects.filter(baz=True)
)
}}}
Given that this looks very like a runtime-evaluated queryset, I suggest a
docs amendment that clarifies the behaviour. (I am not confident in making
that docs amendment myself, at least until I have properly understood the
intended behaviour.)
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:1>
* owner: nobody => ChillarAnand
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:2>
* needs_docs: 0 => 1
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:3>
Comment (by ChillarAnand):
Submitted [https://github.com/django/django/pull/7596 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:4>
Comment (by Nick Smith):
PR above looks like the wrong link. Did you mean
https://github.com/django/django/pull/7956?
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"a364fb38107534362a9817d7d3243d4695798ffd" a364fb3]:
{{{
#!CommitTicketReference repository=""
revision="a364fb38107534362a9817d7d3243d4695798ffd"
[1.11.x] Fixed #27501 -- Documented lazy evaluation of
ModelChoiceField.queryset.
Backport of d9aeee205d93b12c96da449c64c1f17ca8786086 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"d9aeee205d93b12c96da449c64c1f17ca8786086" d9aeee20]:
{{{
#!CommitTicketReference repository=""
revision="d9aeee205d93b12c96da449c64c1f17ca8786086"
Fixed #27501 -- Documented lazy evaluation of ModelChoiceField.queryset.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:6>