在 Django 中搜索并使用多个单词 GET
如果我想做这样的事情,你能告诉我应该使用什么或在哪里查看吗:当有人在搜索字段中输入“aaa bbb”(?t=aaa+bbb)时,它只会找到那些模型,其中标题字段是“aaa bbb”,但不是“aaa ccc bbb”。例如,如何更改此代码以使其找到所有标题,其中标题是“aaa”或“bbb”单词?
if 't' in request.GET:
search = request.GET['t']
result = somemodel.objects.filter(Title__icontains = search).order_by('-pub_date')
或者标题中有“aaa”和“bbb”单词,但不完全是一个接一个?我应该将“图标”更改为其他内容吗?或者做一些循环将“aaa bbb”拆分为“aaa”和“bbb”(如果是) - 如何?)
Could you tell me what should I use or where to look if I want to make something like this: When someone types "aaa bbb" (?t=aaa+bbb) in search field, it would only find those models, in which Title field is "aaa bbb", but not "aaa ccc bbb". How to change for example this code to make it find all titles, in which Titles is "aaa" or "bbb" word?
if 't' in request.GET:
search = request.GET['t']
result = somemodel.objects.filter(Title__icontains = search).order_by('-pub_date')
Or in Title are "aaa" and "bbb" words, but not exatcly one after another? Should I change "icontains" to something else? Or make some loop to split "aaa bbb" into "aaa" and "bbb" if yes - how?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像这个一样,但是在空格上分割(
.split()
) 并使用Q
对象中的适当字段。Like this, but split on whitespace (
.split()
) and use the appropriate field in theQ
objects.