Opened 8 years ago

Last modified 7 years ago

#27501 closed Cleanup/optimization

ModelChoiceField.queryset documentation isn't clear that this is lazily evaluated — at Version 1

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 (last modified by Tim Graham)

The 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
  2. edit eligible related objects in the db
  3. refresh form (without restarting the server)
  4. 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 (1)

comment:1 by Tim Graham, 8 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top