How to dynamically compose an OR query filter in Django?

后端 未结 14 1340
清歌不尽
清歌不尽 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:25

    You can use the |= operator to programmatically update a query using Q objects.

    0 讨论(0)
  • 2021-01-22 06:26

    See the docs:

    >>> Blog.objects.in_bulk([1])
    {1: <Blog: Beatles Blog>}
    >>> Blog.objects.in_bulk([1, 2])
    {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>}
    >>> 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])
    
    0 讨论(0)
提交回复
热议问题