如何申请包含在django中的列表?

时间:2017-12-29 13:14:18

标签: django filter model django-queryset

我想搜索过滤后的对象。

以下是过滤后的对象。

questions = models.Question.objects.filter(creator=user, tags__name__in=hashtags)

我尝试了以下命令。但它不起作用。

questions = models.Question.objects.filter(tags__name__contains=hashtags)

我认为__contains似乎不适用于列表

P.S。 hashtags是一个列表

1 个答案:

答案 0 :(得分:1)

您可以使用Q对象:

from django.db.models import Q

for tag in hashtags:
    conditions |= Q(tags__name__contains=tag)
questions = models.Question.objects.filter(conditions)