Jonas
Jonas

Reputation: 11

How do I tell django to not escape % and _ in a query

I want to be able to use wildcards in my django queries used for searching. However as the documentation says:

Entry.objects.filter(headline__contains='%')

Will result in SQL that looks something like this:

SELECT ... WHERE headline LIKE '%\%%';

How do I tell django to not escape % and _ in a query. Or is there another way to implement wildcard search in django (apart from writing the sql directly)?

Upvotes: 1

Views: 1196

Answers (2)

Ned Batchelder
Ned Batchelder

Reputation: 375484

headline__contains='%' would mean headline is anything, no? In which case why include it in the query?

Upvotes: 1

muksie
muksie

Reputation: 13043

You can use the extra() method to insert a custom where clause:

Entry.objects.extra(where="headline LIKE '%'")

Upvotes: 0

Related Questions