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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701916887dc7f-0784abea-385f-495f-b3f1-d78964825350-000000%40eu-central-1.amazonses.com.

Reply via email to