Skip to content

Add support for chaining in_bulk() after values()/values_list() #74

@jacobtylerwalls

Description

@jacobtylerwalls

Code of Conduct

  • I agree to follow Django's Code of Conduct

Feature Description

Add support for chaining QuerySet.in_bulk() after .values()/.values_list().

This was recently updated to raise TypeError when attempted.

Problem

in_bulk() is useful for more than just aliasing {obj.pk: obj for obj in qs}, since it can apply a filter clause based on an id_list input, and batch according to the database's parameter limit. That limit can be cumbersome to calculate, especially after support for composite pk's in Django 5.2.

For instance, in the model field fetch modes feature, the implementation of fetch_peers() issues a .filter() against a potentially large set of pk's. It would be convenient to leverage .in_bulk(id_list=pks) to do this filtering in a parameter-limit-safe way, but the queryset is a values() queryset, since we don't want to materialize the entire objects, so we... can't.

See comment.

Request or proposal

proposal

Additional Details

No response

Implementation Suggestions

There's a draft implementation for at least values() by @john-parton here .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Django CoreThis idea is suitable for inclusion in Django itselfModels/ORM

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions