1

I was asked to create the backend of a project using Django (the frontend is angular). So I thought about using rest framework but I'm a total beginner and raw sql queries are needed for this project. To be more precise it's a complex query that needs many tables: they provided the sql script that I need to use it directly.

My question is does rest framework allow such raw queries (because I was not able to find a tutorial about that) or do I need something else?

Edit for further explanation: I'm I able to return complex queries in rest framework as a GET request for example? Any help is appreciated.

1

2 Answers 2

1

You can use the raw function to make it

Model.objects.raw("WRITE YOUR SQL HERE")
Sign up to request clarification or add additional context in comments.

3 Comments

Does this work in rest framework? my main problem is how to return the result of the query as a GET request for example
this doesn't have any relation with rest framework it's the same as using filter
add more details ... what do you want to do?
0

You can use cursor connection for direct execute SQL query.

from django.db import connection

def my_query(self):
    with connection.cursor() as cursor:
    cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
    cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    row = cursor.fetchall()

return row

1 Comment

Does this work in rest framework? my main problem is how to return the result of the query as a GET request for example

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.