#27501 closed Cleanup/optimization (fixed)
ModelChoiceField.queryset documentation isn't clear that this is lazily evaluated
| Reported by: | Nick Smith | Owned by: | ChillarAnand |
|---|---|---|---|
| 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 (last modified by )
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.)
Change History (6)
comment:1 by , 9 years ago
| Description: | modified (diff) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:3 by , 9 years ago
| Has patch: | set |
|---|---|
| Needs documentation: | set |
Submitted PR