在Django中,如何找到一个字符串的一部分而不是包含该字符串? [英] In Django, how to find a term that is part of a string instead of containing that string?

查看:1336
本文介绍了在Django中,如何找到一个字符串的一部分而不是包含该字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在我的表中的两个字段下有三行像这样

For example, there are three rows under two fields in my table like this

id name
-------
1  brown cat
2  black dog
3  person


$ b b

这里有一个字符串或一个句子:一个棕色的猫跳过一个人,如何使用这个字符串获得第一行和第三行,这是句子的一部分?据我所知, __包含 __ regex 只能搜索包含目标字符串的术语,但如果术语

And here comes a string, or a sentence: A brown cat jumps over a person, how can I use this string to get the first row and third row, which is part of this sentence? As I understand, __contains and __regex can only search the terms which contains a target string but will not work if the term is part of the target string.. Does anyone have ideas about this?

推荐答案

以下查询将执行:

>>> sentence = 'A brown cat jumps over a person'
>>> MyModel.objects.extra(where={"%s like CONCAT('%%', `name` ,'%%')"}, 
...                       params=[sentence]).values()
[{'id': 1L, 'name': u'brown cat'}, {'id': 3L, 'name': u'person'}]

这篇关于在Django中,如何找到一个字符串的一部分而不是包含该字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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