Refs #36605 -- Optimized QuerySet.in_bulk() for the empty id_list case.
Some checks failed
Linters / flake8 (push) Waiting to run
Linters / isort (push) Waiting to run
Linters / black (push) Waiting to run
Tests / Windows, SQLite, Python 3.13 (push) Waiting to run
Tests / JavaScript tests (push) Waiting to run
Docs / spelling (push) Has been cancelled
Docs / blacken-docs (push) Has been cancelled
Docs / lint-docs (push) Has been cancelled

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:
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
unique_fields = [
constraint.fields[0]
@ -1236,8 +1238,6 @@ class QuerySet(AltersData):
)
if id_list is not None:
if not id_list:
return {}
filter_key = "{}__in".format(field_name)
id_list = tuple(id_list)
batch_size = connections[self.db].ops.bulk_batch_size([opts.pk], id_list)