#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
--------------------------------------+------------------------------------
Reporter: Nick Smith | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):
* 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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.8743e0c13f0ac1eff64ee605605c415f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.