From 3159ad4df6ec05c3eac07b930b4af32739bec7b0 Mon Sep 17 00:00:00 2001 From: Tom Carrick Date: Tue, 28 Mar 2017 16:57:23 +0100 Subject: Fixed #27970 -- Allowed QuerySet.in_bulk() to fetch on fields besides primary key. --- docs/ref/models/querysets.txt | 16 ++++++++++++---- docs/releases/2.0.txt | 3 +++ 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'docs') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 2c1f68fc06..74f83ab8c5 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1997,11 +1997,13 @@ database query like ``count()`` would. ``in_bulk()`` ~~~~~~~~~~~~~ -.. method:: in_bulk(id_list=None) +.. method:: in_bulk(id_list=None, field_name='pk') -Takes a list of primary-key values and returns a dictionary mapping each -primary-key value to an instance of the object with the given ID. If a list -isn't provided, all objects in the queryset are returned. +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. Example:: @@ -2013,9 +2015,15 @@ Example:: {} >>> Blog.objects.in_bulk() {1: , 2: , 3: } + >>> Blog.objects.in_bulk(['beatles_blog'], field_name='slug') + {'beatles_blog': } If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary. +.. versionchanged:: 2.0 + + The ``field_name`` parameter was added. + ``iterator()`` ~~~~~~~~~~~~~~ diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 16024ae244..162ac42306 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -259,6 +259,9 @@ Models :meth:`~.QuerySet.select_for_update()` is used in conjunction with :meth:`~.QuerySet.select_related()`. +* The new ``field_name`` parameter of :meth:`.QuerySet.in_bulk` allows fetching + results based on any unique model field. + Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.3