Я пробовал это:
from django.db.models import Q
# first you get the IDs
ids = [1,2,3,4,5]
# after that you can get a list of Q objects
q_objects = [Q(id=id) for id in ids]
# then you need to combine all the Q objects
# I'll use a functional approach, an operation called fold_left but you can use a for instead
# i need the first element
first_q = q_objects.pop()
final_q = reduce(lambda total,q: total | q,q_objects,first_q)
# now you can get the result
MyModel.objects.filter(q)
#if you need an order simply append order_by
MyModel.objects.filter(q).order_by(something)
В любом случае, посмотрите на запрос, выполняемый in_bulk
, потому что он использует предложение IN SQL. Это тест с django 1.3 и MySQL:
python manage.py shell
>>> from testapp.models import Car
>>> from django.db import connection
>>> cars = Car.objects.in_bulk([1,2])
>>> my_queries = connection.queries
>>> my_queries[0]['sql']
Это результат SQL:
SELECT [fields]
FROM [my_table] WHERE [my_model].`id` IN (1, 2)
Конечно, вы можете использовать только django_toolbar.