From b9be11d44265308863e4e8cfb458cd3605091452 Mon Sep 17 00:00:00 2001 From: Kaustubh <55352418+kc611@users.noreply.github.com> Date: Sat, 22 Aug 2020 11:23:50 +0530 Subject: Fixed #31918 -- Allowed QuerySet.in_bulk() to fetch on a single distinct field. --- docs/ref/models/querysets.txt | 11 +++++++++-- docs/releases/3.2.txt | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index e5c0038528..228e2cf736 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -2250,8 +2250,9 @@ database query like ``count()`` would. Takes a list of field values (``id_list``) and the ``field_name`` for those values, and returns a dictionary mapping each value to an instance of the object with the given field value. If ``id_list`` isn't provided, all objects -in the queryset are returned. ``field_name`` must be a unique field, and it -defaults to the primary key. +in the queryset are returned. ``field_name`` must be a unique field or a +distinct field (if there's only one field specified in :meth:`distinct`). +``field_name`` defaults to the primary key. Example:: @@ -2265,9 +2266,15 @@ Example:: {1: , 2: , 3: } >>> Blog.objects.in_bulk(['beatles_blog'], field_name='slug') {'beatles_blog': } + >>> Blog.objects.distinct('name').in_bulk(field_name='name') + {'Beatles Blog': , 'Cheddar Talk': , 'Django Weblog': } If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary. +.. versionchanged:: 3.2 + + Using a distinct field was allowed. + ``iterator()`` ~~~~~~~~~~~~~~ diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index c7219c759b..d29a582d74 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -286,6 +286,10 @@ Models * The new :class:`~django.db.models.functions.Collate` function allows filtering and ordering by specified database collations. +* The ``field_name`` argument of :meth:`.QuerySet.in_bulk()` now accepts + distinct fields if there's only one field specified in + :meth:`.QuerySet.distinct`. + Pagination ~~~~~~~~~~ -- cgit v1.3