在 Django 中搜索并使用多个单词进行 GET [英] Search in Django and GET with multiple words

查看:38
本文介绍了在 Django 中搜索并使用多个单词进行 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想做这样的事情,你能告诉我应该使用什么或在哪里查看:当有人在搜索字段中输入aaa bbb"(?t=aaa+bbb) 时,它只会找到那些模型,其中标题字段是aaa bbb",而不是aaa ccc 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')

或者在Title中有aaa"和bbb"两个词,但不是一个接一个?我应该将icontains"更改为其他内容吗?或者做一些循环将aaa bbb"拆分为aaa"和bbb",如果是的话 - 如何?)

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?)

推荐答案

Like this,但在空白处拆分(.split())并使用 Q 对象中的适当字段.

Like this, but split on whitespace (.split()) and use the appropriate field in the Q objects.

这篇关于在 Django 中搜索并使用多个单词进行 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆