Skip to main content

How to write SQL-like Query in Django ORM

Learn how to use Django ORM via SQL-like Queries

In Django, you can use the __icontains lookup to perform a SQL LIKE query to filter results based on a substring match. This lookup allows you to search for records that contain a specific string anywhere within a text field.

How to write SQL-like Query in Django ORM - Tutorial provided by AppSeed.

Here's how you can write a SQL LIKE query using Django ORM:

Assuming you have a Django model called MyModel and you want to perform a LIKE query on the field_name field:

from myapp.models import MyModel

# Use the __icontains lookup to perform a SQL LIKE query
results = MyModel.objects.filter(field_name__icontains='search_term')

In the code above:

  • MyModel is your Django model.
  • field_name is the name of the field you want to search within.
  • 'search_term' is the substring you want to search for.

The filter method returns a QuerySet containing all records that match the field_name containing the specified search_term.

The __icontains lookup performs a case-insensitive search, so it will match records regardless of the case of the search_term.

You can chain additional filters or query conditions to further refine your query. For example:

results = MyModel.objects.filter(field_name__icontains='search_term', another_field=some_value)

This query will return records where field_name contains the search_term and another_field is equal to some_value.

Remember to replace MyModel, field_name, 'search_term', and any other specific values with your actual model, field name, and search criteria.

✅ Resources​