Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 576
Aswin Murugesh Avatar asked Aug 09 '13 06:08

Aswin Murugesh


People also ask

What is the command to execute the SQL equivalent to Django model?

The raw() manager method can be used to perform raw SQL queries that return model instances: Manager. raw (raw_query, params=(), translations=None) This method takes a raw SQL query, executes it, and returns a django.

Can you use SQL in Django?

Django gives you two ways of performing raw SQL queries: you can use Manager.raw() to perform raw queries and return model instances, or you can avoid the model layer entirely and execute custom SQL directly. Explore the ORM before using raw SQL!

Is Django same as SQL?

Django and SQL are not replacements for one another, Django is a web framework as a whole, designed to develop web applications, and SQL is a language to query databases.


1 Answers

Use __contains or __icontains (case-insensitive):

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

The SQL equivalent is

SELECT ... WHERE string LIKE '%pattern%'; 
like image 114
falsetru Avatar answered Oct 14 '22 01:10

falsetru