Django filter queryset using a list

时间:2019-03-17 22:21:31

标签: django django-queryset

I have one table, table A that holds primary keys of all the objects in the table b.

if I store all the primary keys from table A in a list, then is it possible to get a list of all the objects in table B using the primary keys I have stored in a list.

For Example:

tableB.objects.filter(pks = list) 

is there any way I can construct a query set like the above that would get me a list of all the object with the primary keys?

1 个答案:

答案 0 :(得分:6)

tableB.objects.filter(pk__in=list)

Docs: https://docs.djangoproject.com/en/2.1/ref/models/querysets/#in