What is simplest way join __contains and __in?

前端 未结 2 1026
一个人的身影
一个人的身影 2021-01-23 03:09

I am doing tag search function, user could observe a lot of tags, I get it all in one tuple, and now I would like to find all text which include at least one tag from the list.

2条回答
  •  猫巷女王i
    2021-01-23 03:54

    Here you go:

    filter = Q()
    for t in tag_tuple: 
       filter = filter | Q(data__contains=t)
    return text.objects.filter(filter)
    

    A couple of tips:

    • You should be naming your model classes with a capital (i.e. Text, not text)
    • You may want __icontains instead if you're not worried about the case

提交回复
热议问题