SQL “LIKE” equivalent in django Filter Query

sql “LIKE” equivalent in django query:

What is the equivalent of this SQL statement in django? Now we will check this.

SELECT * FROM table_name WHERE string LIKE pattern;

For the Like Statement in Django,

** For case sensitive search

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

fieldname = Table Matching Field Name
pattern = Matching Pattern, Like “Fruct”

by using “__contains” we check if that fieldname contains the pattern.

** For case insensitive search

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

__icontains = For case insensitive

__contains = For case sensitive


** Along with them, you might need these ones with similar behavior:

__startswith = For matching the start string (case sensitive)

__istartswith = For matching the start string (case insensitive)

__endswith = For matching the end string (case sensitive)

__iendswith = For matching the end string (case insensitive)

__contains = For matching the full string (case sensitive)

__icontains = For matching the full string (case insensitive)


Leave a Comment