[Django] #27501: ModelChoiceField.queryset documentation isn't clear that this is lazily evaluated

39 views
Skip to first unread message

Django

unread,
Nov 17, 2016, 7:17:33 AM11/17/16
to django-...@googlegroups.com
#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.

Django

unread,
Nov 17, 2016, 2:14:09 PM11/17/16
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
--------------------------------------+------------------------------------

Reporter: Nick Smith | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Old description:

> 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.)

New description:

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

#22841 also supports this theory, (although the rather old #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#comment:1>

Django

unread,
Jan 26, 2017, 3:06:59 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ChillarAnand):

* owner: nobody => ChillarAnand
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:2>

Django

unread,
Jan 26, 2017, 3:08:10 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ChillarAnand):

* needs_docs: 0 => 1
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:3>

Django

unread,
Jan 26, 2017, 3:10:13 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ChillarAnand):

Submitted [https://github.com/django/django/pull/7596 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:4>

Django

unread,
Jan 26, 2017, 3:16:25 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nick Smith):

PR above looks like the wrong link. Did you mean
https://github.com/django/django/pull/7956?

--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:5>

Django

unread,
Jan 26, 2017, 6:58:55 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.10
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"a364fb38107534362a9817d7d3243d4695798ffd" a364fb3]:
{{{
#!CommitTicketReference repository=""
revision="a364fb38107534362a9817d7d3243d4695798ffd"
[1.11.x] Fixed #27501 -- Documented lazy evaluation of
ModelChoiceField.queryset.

Backport of d9aeee205d93b12c96da449c64c1f17ca8786086 from master
}}}

Django

unread,
Jan 26, 2017, 6:59:04 PM1/26/17
to django-...@googlegroups.com
#27501: ModelChoiceField.queryset documentation isn't clear that this is lazily
evaluated
-------------------------------------+-------------------------------------
Reporter: Nick Smith | Owner:
Type: | ChillarAnand
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.10

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"d9aeee205d93b12c96da449c64c1f17ca8786086" d9aeee20]:
{{{
#!CommitTicketReference repository=""
revision="d9aeee205d93b12c96da449c64c1f17ca8786086"


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

--
Ticket URL: <https://code.djangoproject.com/ticket/27501#comment:6>

Reply all
Reply to author
Forward
0 new messages