Antu
Antu

Reputation: 2323

how to write raw like query in django2?

I was trying to develop a search application where a user can search and for my system requirement I need to avoid ORM query when I try to write this following raw query

q = request.POST.get('searchData')
    if q:
        titleInfo = Item.objects.raw("""select * from item where title like '%%s%'""", [q])

It gives me this error

ValueError at /test
unsupported format character ''' (0x27) at index 41

And if I remove the quotation

"""select * from item where title like %%s%"""

It gives me the following error

ValueError at /test
incomplete format

Where my query is working fine in MySQL database

Upvotes: 1

Views: 60

Answers (1)

Antu
Antu

Reputation: 2323

I solve the problem like this

q = request.POST.get('searchData')
    query = '%'+q+'%'
    if q:
        titleInfo = Item.objects.raw("select * from item where title like %s", [query])

Upvotes: 1

Related Questions