How to dynamically compose an OR query filter in Django?

后端 未结 14 1362
清歌不尽
清歌不尽 2021-01-22 05:24

From an example you can see a multiple OR query filter:

Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))

For example, this results in:

14条回答
  •  广开言路
    2021-01-22 06:26

    See the docs:

    >>> Blog.objects.in_bulk([1])
    {1: }
    >>> Blog.objects.in_bulk([1, 2])
    {1: , 2: }
    >>> Blog.objects.in_bulk([])
    {}
    

    Note that this method only works for primary key lookups, but that seems to be what you're trying to do.

    So what you want is:

    Article.objects.in_bulk([1, 2, 3])
    

提交回复
热议问题