1
0
mirror of https://github.com/django/django.git synced 2025-10-09 14:59:24 +00:00

Refs #36605 -- Optimized QuerySet.in_bulk() for the empty id_list case.

Now that the setup is a bit more expensive, it makes sense to return earlier
for the empty case.
This commit is contained in:
Adam Johnson 2025-09-11 11:49:54 +01:00 committed by Jacob Walls
parent 1820d35b17
commit 46bd92274c

View File

@ -1166,6 +1166,8 @@ class QuerySet(AltersData):
""" """
if self.query.is_sliced: if self.query.is_sliced:
raise TypeError("Cannot use 'limit' or 'offset' with in_bulk().") raise TypeError("Cannot use 'limit' or 'offset' with in_bulk().")
if id_list is not None and not id_list:
return {}
opts = self.model._meta opts = self.model._meta
unique_fields = [ unique_fields = [
constraint.fields[0] constraint.fields[0]
@ -1236,8 +1238,6 @@ class QuerySet(AltersData):
) )
if id_list is not None: if id_list is not None:
if not id_list:
return {}
filter_key = "{}__in".format(field_name) filter_key = "{}__in".format(field_name)
id_list = tuple(id_list) id_list = tuple(id_list)
batch_size = connections[self.db].ops.bulk_batch_size([opts.pk], id_list) batch_size = connections[self.db].ops.bulk_batch_size([opts.pk], id_list)