0

I need to get raw objects using Django .objects.raw functions like :

SELECT * FROM TEST_APP_DOCUMENT WHERE DATE BETWEEN to_date('0000-02-07','YYYY-MM-DD') AND to_date('2027-02-15', 'YYYY-MM-DD')

in pgAdmin select return good result, but when i put it to django there is an error:

  File "C:\Users\User\Desktop\test_task\test_app\views.py", line 110
    queryset = Document.objects.raw('SELECT * FROM TEST_APP_DOCUMENT WHERE DATE BETWEEN to_date('0000 - 02 - 07','YYYY - MM - DD') AND to_date('2027 - 02 - 15', 'YYYY - MM - DD')')
                                                                                                    ^
SyntaxError: invalid syntax

what is the syntax problem?

0

1 Answer 1

1

You're passing a string literal with single parenthesis (') while having them inside the string itself. You must escape them or use ":

queryset = Document.objects.raw("SELECT * FROM TEST_APP_DOCUMENT WHERE DATE BETWEEN to_date('0000 - 02 - 07','YYYY - MM - DD') AND to_date('2027 - 02 - 15', 'YYYY - MM - DD')")

You can escape with backslash, as you'd expect. For example, 'foo\'bar' will yield foo'bar.

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

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.