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?
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.
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!
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.
Use __contains
or __icontains
(case-insensitive):
result = table.objects.filter(string__contains='pattern')
The SQL equivalent is
SELECT ... WHERE string LIKE '%pattern%';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With