问题:Django查询中的“ LIKE”等效SQL

django中的此SQL语句等效于什么?

SELECT * FROM table_name WHERE string LIKE pattern;

如何在Django中实现呢?我试过了

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

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

What is the equivalent of this SQL statement in django?

SELECT * FROM table_name WHERE string LIKE pattern;

How do I implement this in django? I tried

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

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


回答 0

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

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

SQL等效为

SELECT ... WHERE string LIKE '%pattern%';

Use __contains or __icontains (case-insensitive):

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

The SQL equivalent is

SELECT ... WHERE string LIKE '%pattern%';

回答 1

由falsetru提及的包含和icontains使查询类似 SELECT ... WHERE headline LIKE '%pattern%

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

制造

SELECT ... WHERE headline LIKE 'pattern%

要么

SELECT ... WHERE headline LIKE '%pattern

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

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

making

SELECT ... WHERE headline LIKE 'pattern%

or

SELECT ... WHERE headline LIKE '%pattern


回答 2

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

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

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

Case insensitive search for string in a field.


回答 3

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

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

字符串方法是不可变的,因此您的模式变量不会更改,并且使用。*时,您会寻找0个或多个出现的任何字符,但要换行。

通过使用以下代码遍历模式词:

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

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

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

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.

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.


回答 4

这可以通过Django的自定义查询来完成。我已经将查询转换为Django式lookup应用程序。安装后__like,使用%和查找_将启用通配符。

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

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

This can be done with Django’s custom lookups. I have made the lookup into a Django-like-lookup application. After installing it the __like lookup with the % and _ wildcards will be enabled.

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。