[Django] #31879: Changing a model formset's queryset has no effect when self.forms is accessed

48 views
Skip to first unread message

Django

unread,
Aug 12, 2020, 9:58:49 AM8/12/20
to django-...@googlegroups.com
#31879: Changing a model formset's queryset has no effect when self.forms is
accessed
-----------------------------------------+--------------------------
Reporter: kemar | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Keywords: formsets
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+--------------------------
The Model formsets documentation
[https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#changing-
the-queryset suggests a way to override the default queryset]:

{{{
#!python
class BaseAuthorFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = Author.objects.filter(name__startswith='O')
}}}

However, accessing `self.forms` anywhere before `self.queryset` will
trigger
[https://github.com/django/django/blob/e74b3d724e5ddfef96d1d66bd1c58e7aae26fc85/django/forms/models.py#L629
get_queryset()] and silently populate `self._queryset` with all objects in
the model so that the next `self.queryset` will have no effect:

{{{
#!python
class BaseAuthorFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
self.queryset = Author.objects.filter(name__startswith='O') # No
effect.
}}}

Can't tell if it's a bug or an intended behaviour?

--
Ticket URL: <https://code.djangoproject.com/ticket/31879>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 13, 2020, 5:13:02 AM8/13/20
to django-...@googlegroups.com
#31879: Changing a model formset's queryset has no effect when self.forms is
accessed
-------------------------------+--------------------------------------
Reporter: kemar | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: formsets | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

This is expected behaviour. We call `get_queryset()` when generating the
forms — if you generate them before setting the `queryset` attribute then
it'll be too late to have effect. The underlying `_queryset` cache is an
optimisation for almost all use-cases. It can be cleared, or you can
override `get_queryset()` if needed.

Please don't use the issue tracker for this kind of usage question. See
TicketClosingReasons/UseSupportChannels. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/31879#comment:1>

Django

unread,
Aug 13, 2020, 5:26:32 AM8/13/20
to django-...@googlegroups.com
#31879: Changing a model formset's queryset has no effect when self.forms is
accessed
-------------------------------+--------------------------------------
Reporter: kemar | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: formsets | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by kemar):

Understood. Thank you for taking the time to answer so carefully.

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

Reply all
Reply to author
Forward
0 new messages