[Django] #33841: Django shouldn't fail (bulk-)update silently when QuerySet is already evaluated / update documentation

3 views
Skip to first unread message

Django

unread,
Jul 12, 2022, 6:10:27 PM7/12/22
to django-...@googlegroups.com
#33841: Django shouldn't fail (bulk-)update silently when QuerySet is already
evaluated / update documentation
-------------------------------------+-------------------------------------
Reporter: SHxKM | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 3.2
layer (models, ORM) | Keywords: update, bulk-
Severity: Normal | update, queryset
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The following code:

{{{
initial_queryset = MyObject.objects.filter(...)
filtered_queryset = initial_queryset.exclude(related_model__id__in=...)
for_loop_on_filtered = [MyOtherModel(some_id=..., other_id=...) for x in
filtered_queryset]
MyOtherModel.objects.bulk_create(for_loop_on_filtered)
filtered_queryset.update(processed=True) # silent failure to update
}}}

Fails to update all objects in `filtered_queryset`. It seems like the
reason is the list-comprehension on `filtered_queryset`.
I've been with Django for >6 years and this is by far the most surprising
behavior I've seen.

It does seem to be documented (?):

"You cannot call update() on a QuerySet that has had a slice taken or can
otherwise no longer be filtered."

But the fact is I can and did call `update()` on that QuerySet, and Django
returned 0 errors.

If that is the intended behavior, then I suggest updating the
documentation.

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

Django

unread,
Jul 13, 2022, 1:44:09 AM7/13/22
to django-...@googlegroups.com
#33841: Django shouldn't fail (bulk-)update silently when QuerySet is already
evaluated / update documentation
-------------------------------------+-------------------------------------
Reporter: Shibel Karmi | Owner: nobody
Mansour |
Type: Uncategorized | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: update, bulk- | Triage Stage:
update, queryset | Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

Thanks for the report, however it works for me. Maybe in
`initial_queryset` you filter out conditions that don't match any rows
after adding `MyOtherModel` instances, it's hard to tell. Please use one
of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] to debug your issue.

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

Reply all
Reply to author
Forward
0 new messages