当前位置: 动力学知识库 > 问答 > 编程问答 >

python - sql "LIKE" equivalent in django query

问题描述:

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?

网友答案:

Use contains or icontains:

result = table.objects.filter(string__contains='pattern')
网友答案:
result = table.objects.filter(string__icontains='pattern')

Case insensitive search for string in a field.

网友答案:

contains and icontains mentioned by falsetrue 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

分享给朋友:
您可能感兴趣的文章:
随机阅读: