1

I've tried the following query with Django,

def search(search_text):
    q  = Info.objects.filter(title__contains=str(search_text))
    print q.query

The query that get printed is

SELECT "d"."id", "d"."foo" FROM "d_info" WHERE "d_info"."title" LIKE %hello% ESCAPE '\' 

The query fails because the text after LIKE doesn't have quotes around it. The query succeeds when I run the query on the sql prompt with quotes around the text after LIKE like below

SELECT "d"."id", "d"."foo" FROM "d_info" WHERE "d_info"."title" LIKE '%hello%' ESCAPE '\' 

How do I get Django to add the quotes around the search_text so that the query succeeds ?

I'm using Djanog with sqlite3

2 Answers 2

1

I tried this out with Postgresql 8.3. The query is generated without quotes. However executing the filter returns a valid queryset with expected instances. Can you try executing

q = Info.objects.filter(title__contains=str(search_text))
print q.count()

and see if it works?

Sign up to request clarification or add additional context in comments.

3 Comments

It isn't different from what I have. Did you mean something else ?
I meant to ask, did you execute the query behind the (lazy) queryset? Print q.count() will tell you if the query fired OK.
Sorry I missed the 2nd statement, it so turns out the query works within Django, but when asked to print the query and if I execute the same query in a mysql shell or sqlite shell, it doesn't work. Django is probably printing the query wrong.
0

Posting my comment above as the answer

It so turns out the query works within Django, but when asked to print the query and if I copy the query printed and execute it in a mysql shell or sqlite shell, it doesn't work. Django is probably printing the query wrong

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.