#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-----------------------------------------+------------------------
Reporter: Nick Smith | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
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.)
--
Ticket URL: <https://code.djangoproject.com/ticket/27501>
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/050.a12fa8d6f3fbc130eefbd7313724a717%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.