gpt4 book ai didi

python - 对象.filter ("ı wanna add two contains field")

转载 作者:行者123 更新时间:2023-12-01 07:49:17 26 4
gpt4 key购买 nike

这是搜索字段。但在 View 中我无法在两个字段上搜索。我尝试了这一切。它不工作。它只适用于 = Makale.objects.filter(baslik__contains=keyword) 等一个字段

makale =  Makale.objects.filter(baslik__contains=keyword,icerik_contains=keyword)

makale = Makale.objects.filter(baslik_contains=keyword or icerik_contains=keyword)

def paylasimlar(request):
keyword = request.GET.get("keyword")
if keyword:
paylasimlar = Makale.objects.filter(icerik__contains=keyword)
return render(request, "feed.html", {"paylasimlar": paylasimlar})
paylasimlar = Makale.objects.all()
return render(request, "feed.html", {"paylasimlar":paylasimlar})

最佳答案

您可以使用 Q-objects [Django-doc] 在两个约束之间添加运算符。和 or (|) operator [Django-doc] ,例如:

from django.db.models import <b>Q</b>

Makale.objects.filter(<b>Q(</b>baslik__contains=keyword<b>) | Q(</b>icerik__contains=keyword<b>)</b>)

关于python - 对象.filter ("ı wanna add two contains field"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56308308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com