sql "LIKE" equivalent in django query

Aswin Murugesh picture Aswin Murugesh · Aug 9, 2013 · Viewed 89.3k times · Source

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?

Answer

falsetru picture falsetru · Aug 9, 2013

Use __contains or __icontains (case-insensitive):

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

The SQL equivalent is

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