Python django 查询中的 sql“LIKE”等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18140838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:59:19  来源:igfitidea点击:

sql "LIKE" equivalent in django query

pythonsqldjangodjango-modelsdjango-queryset

提问by Aswin Murugesh

What is the equivalent of this SQL statement in django?

django 中这条 SQL 语句的等价物是什么?

SELECT * FROM table_name WHERE string LIKE pattern;

How do I implement this in django? I tried

我如何在 Django 中实现它?我试过

result = table.objects.filter( pattern in string )

But that did not work. How do i implement this?

但这没有用。我该如何实施?

采纳答案by falsetru

Use __containsor __icontains(case-insensitive):

使用__contains__icontains(不区分大小写):

result = table.objects.filter(string__contains='pattern')

回答by Dmitriy Kuznetsov

contains and icontains mentioned by falsetru make queries like SELECT ... WHERE headline LIKE '%pattern%

falsetru 提到的 contains 和 icontains 进行查询,例如 SELECT ... WHERE headline LIKE '%pattern%

Along with them, you might need these ones with similar behavior: startswith, istartswith, endswith, iendswith

与它们一起,您可能需要具有类似行为的这些: startswithistartswithendswithiendswith

making

制作

SELECT ... WHERE headline LIKE 'pattern%

SELECT ... WHERE headline LIKE 'pattern%

or

或者

SELECT ... WHERE headline LIKE '%pattern

SELECT ... WHERE headline LIKE '%pattern

回答by Venkat Kotra

result = table.objects.filter(string__icontains='pattern')

Case insensitive search for string in a field.

在字段中搜索字符串不区分大小写。

回答by Rodrigo ávila

In order to preserve the order of the words as in the sql LIKE '%pattern%' statement I use iregex, for example:

为了保留 sql LIKE '%pattern%' 语句中的单词顺序,我使用 iregex,例如:

qs = table.objects.filter(string__iregex=pattern.replace(' ', '.*'))

string methods are immutable so your pattern variable will not change and with .* you'll be looking for 0 or more occurrences of any character but break lines.

字符串方法是不可变的,因此您的模式变量不会更改,并且使用 .* 您将查找 0 次或多次出现的任何字符,但换行符除外。

By using the following to iterate over the pattern words:

通过使用以下内容来迭代模式词:

qs = table.objects
for word in pattern.split(' '):
    qs = qs.filter(string__icontains=word)

the order of the words in your pattern will not be preserved, for some people that could work but in the case of trying to mimic the sql like statement I'll use the first option.

不会保留模式中单词的顺序,对于某些可以工作但在尝试模仿 sql like 语句的情况下,我将使用第一个选项。

回答by Petr Dlouhy

This can be done with Django's custom lookups. I have made the lookup into a Django-like-lookup application. After installing it the __likelookup with the %and _wildcards will be enabled.

这可以通过Django 的自定义查找来完成。我已经将查找变成了一个类似 Django 的查找应用程序。安装后,将启用__like使用%_通配符进行查找。

All the necessary code in the application is:

应用程序中所有必要的代码是:

from django.db.models import Lookup
from django.db.models.fields import Field


@Field.register_lookup
class Like(Lookup):
    lookup_name = 'like'

    def as_sql(self, compiler, connection):
        lhs, lhs_params = self.process_lhs(compiler, connection)
        rhs, rhs_params = self.process_rhs(compiler, connection)
        params = lhs_params + rhs_params
        return '%s LIKE %s' % (lhs, rhs), params