开发者

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

开发者 https://www.devze.com 2023-01-04 20:17 出处:网络
I want to be able to use wildcards in my django queries used fo开发者_如何学Pythonr searching. However as the documentation says:

I want to be able to use wildcards in my django queries used fo开发者_如何学Pythonr 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)?


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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消