1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #31918 -- Allowed QuerySet.in_bulk() to fetch on a single distinct field.

This commit is contained in:
Kaustubh 2020-08-22 11:23:50 +05:30 committed by Mariusz Felisiak
parent 547a07fa7e
commit b9be11d442
4 changed files with 33 additions and 3 deletions

View File

@ -699,7 +699,8 @@ class QuerySet:
if ( if (
field_name != 'pk' and field_name != 'pk' and
not opts.get_field(field_name).unique and not opts.get_field(field_name).unique and
field_name not in unique_fields field_name not in unique_fields and
not self.query.distinct_fields == (field_name,)
): ):
raise ValueError("in_bulk()'s field_name must be a unique field but %r isn't." % field_name) raise ValueError("in_bulk()'s field_name must be a unique field but %r isn't." % field_name)
if id_list is not None: if id_list is not None:

View File

@ -2250,8 +2250,9 @@ database query like ``count()`` would.
Takes a list of field values (``id_list``) and the ``field_name`` for those 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 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 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 in the queryset are returned. ``field_name`` must be a unique field or a
defaults to the primary key. distinct field (if there's only one field specified in :meth:`distinct`).
``field_name`` defaults to the primary key.
Example:: Example::
@ -2265,9 +2266,15 @@ Example::
{1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>, 3: <Blog: Django Weblog>} {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>, 3: <Blog: Django Weblog>}
>>> Blog.objects.in_bulk(['beatles_blog'], field_name='slug') >>> Blog.objects.in_bulk(['beatles_blog'], field_name='slug')
{'beatles_blog': <Blog: Beatles Blog>} {'beatles_blog': <Blog: Beatles Blog>}
>>> Blog.objects.distinct('name').in_bulk(field_name='name')
{'Beatles Blog': <Blog: Beatles Blog>, 'Cheddar Talk': <Blog: Cheddar Talk>, 'Django Weblog': <Blog: Django Weblog>}
If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary. If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.
.. versionchanged:: 3.2
Using a distinct field was allowed.
``iterator()`` ``iterator()``
~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

View File

@ -286,6 +286,10 @@ Models
* The new :class:`~django.db.models.functions.Collate` function allows * The new :class:`~django.db.models.functions.Collate` function allows
filtering and ordering by specified database collations. 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 Pagination
~~~~~~~~~~ ~~~~~~~~~~

View File

@ -207,6 +207,24 @@ class LookupTests(TestCase):
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
Article.objects.in_bulk([self.au1], field_name='author') Article.objects.in_bulk([self.au1], field_name='author')
@skipUnlessDBFeature('can_distinct_on_fields')
def test_in_bulk_distinct_field(self):
self.assertEqual(
Article.objects.order_by('headline').distinct('headline').in_bulk(
[self.a1.headline, self.a5.headline],
field_name='headline',
),
{self.a1.headline: self.a1, self.a5.headline: self.a5},
)
@skipUnlessDBFeature('can_distinct_on_fields')
def test_in_bulk_multiple_distinct_field(self):
msg = "in_bulk()'s field_name must be a unique field but 'pub_date' isn't."
with self.assertRaisesMessage(ValueError, msg):
Article.objects.order_by('headline', 'pub_date').distinct(
'headline', 'pub_date',
).in_bulk(field_name='pub_date')
@isolate_apps('lookup') @isolate_apps('lookup')
def test_in_bulk_non_unique_meta_constaint(self): def test_in_bulk_non_unique_meta_constaint(self):
class Model(models.Model): class Model(models.Model):