Code

Ticket #1565: in_bulk-empty-list.diff

File in_bulk-empty-list.diff, 776 bytes (added by akaihola, 8 years ago)

patched in_bulk() returns {} if id_list is [] or ()

Line 
1Index: django/db/models/query.py
2===================================================================
3--- django/db/models/query.py   (revision 2598)
4+++ django/db/models/query.py   (working copy)
5@@ -213,7 +213,8 @@
6                 "Cannot use 'limit' or 'offset' with in_bulk"
7         assert isinstance(id_list, (tuple,  list)), "in_bulk() must be provided with a list of IDs."
8         id_list = list(id_list)
9-        assert id_list != [], "in_bulk() cannot be passed an empty ID list."
10+        if id_list == []:
11+            return {}
12         qs = self._clone()
13         qs._where.append("%s.%s IN (%s)" % (backend.quote_name(self.model._meta.db_table), backend.quote_name(self.model._meta.pk.column), ",".join(['%s'] * len(id_list))))
14         qs._params.extend(id_list)