[Django] #35690: Calling queryset.in_bulk() after queryset.values() or queryset.values_list() results in unhelpful error

18 views
Skip to first unread message

Django

unread,
Aug 18, 2024, 10:47:05 PM8/18/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Calling
{{{
MyModel.objects.values().in_bulk()
}}}

Results in a rather annoying error message that doesn't help the user.


{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/john/Code/ecom/.venv/lib/python3.12/site-
packages/django/db/models/query.py", line 1156, in in_bulk
return {getattr(obj, field_name): obj for obj in qs}
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'pk'
}}}

This is because the `in_bulk` method assumes that the queryset has the
default `_iterable_class` (`ModelIterable`) and is attempting to access a
property on an object, but the `_iterable_class` returns dictionaries.

The correct behavior should be either to:

1. Raise a more helpful error if the user attempts to call `in_bulk()`
where the _iterable_class isn't the standard ModelIterable; or
2. Define behavior for when `in_bulk()` is called in that case. (I think
that might be rather challenging, so Behavior 1 is probably the better
option.)
--
Ticket URL: <https://code.djangoproject.com/ticket/35690>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 18, 2024, 10:56:28 PM8/18/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by john-parton):

Pull request here https://github.com/django/django/pull/18496
--
Ticket URL: <https://code.djangoproject.com/ticket/35690#comment:1>

Django

unread,
Aug 18, 2024, 10:56:44 PM8/18/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by john-parton):

* has_patch: 0 => 1

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

Django

unread,
Aug 19, 2024, 12:17:43 AM8/19/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by john-parton):

I've created a second more complicated pull request that implements some
reasonable behavior: https://github.com/django/django/pull/18497
--
Ticket URL: <https://code.djangoproject.com/ticket/35690#comment:3>

Django

unread,
Aug 19, 2024, 2:38:37 AM8/19/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* owner: (none) => john-parton
* stage: Unreviewed => Accepted
* status: new => assigned
* type: Bug => Cleanup/optimization

Comment:

Raising a more helpful error sounds good to me
--
Ticket URL: <https://code.djangoproject.com/ticket/35690#comment:4>

Django

unread,
Sep 2, 2024, 8:22:57 AM9/2/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

Comment:

Note that this has similarities to #26565
[https://github.com/django/django/pull/18496/ PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35690#comment:5>

Django

unread,
Sep 2, 2024, 9:05:02 AM9/2/24
to django-...@googlegroups.com
#35690: Calling queryset.in_bulk() after queryset.values() or
queryset.values_list() results in unhelpful error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"e4a2e22ddbf1b892144b35dc21404c164bc848c6" e4a2e22]:
{{{#!CommitTicketReference repository=""
revision="e4a2e22ddbf1b892144b35dc21404c164bc848c6"
Fixed #35690 -- Errored nicely when using in_bulk() with a values() or
values_list() queryset.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35690#comment:6>
Reply all
Reply to author
Forward
0 new messages