Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#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 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 (6)

comment:1 by Tim Graham, 8 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 by ChillarAnand, 8 years ago

Owner: changed from nobody to ChillarAnand
Status: newassigned

comment:3 by ChillarAnand, 8 years ago

Has patch: set
Needs documentation: set

comment:4 by ChillarAnand, 8 years ago

Submitted PR

Version 0, edited 8 years ago by ChillarAnand (next)

comment:5 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In a364fb3:

[1.11.x] Fixed #27501 -- Documented lazy evaluation of ModelChoiceField.queryset.

Backport of d9aeee205d93b12c96da449c64c1f17ca8786086 from master

comment:6 by Tim Graham <timograham@…>, 8 years ago

In d9aeee20:

Fixed #27501 -- Documented lazy evaluation of ModelChoiceField.queryset.

Note: See TracTickets for help on using tickets.
Back to Top