Django查询:如何使包含或不包含查询

2024-05-07 18:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须进行一个查询,以获取包含“wd2”子字符串或根本不包含“wd”字符串的记录。有什么好办法吗?

好像是:

Record.objects.filter( Q(parameter__icontains="wd2") | Q( ## what should be here? ## ) )


Tags: 字符串objectshereparameter记录befilterrecord
1条回答
网友
1楼 · 发布于 2024-05-07 18:19:36

从djangoq object documentation

You can compose statements of arbitrary complexity by combining Q objects with the & and | operators and use parenthetical grouping. Also, Q objects can be negated using the ~ operator, allowing for combined lookups that combine both a normal query and a negated (NOT) query:

Q(question__startswith='Who') | ~Q(pub_date__year=2005)

所以我建议

Record.objects.filter( Q(parameter__icontains="wd2") | ~Q(parameter__icontains="wd") )

相关问题 更多 >