Opened 8 years ago
Last modified 8 years ago
#27501 closed Cleanup/optimization
ModelChoiceField.queryset documentation isn't clear that this is lazily evaluated — at Initial Version
Reported by: | Nick Smith | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.10 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
The comments in ModelChoiceField._get_choices
suggest that the queryset
attribute is evaluated afresh every time the form is initialised, and testing confirms this:
- load form
- edit eligible related objects in the db
- refresh form (without restarting the server)
- 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:
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.)