In Django, how can you perform raw SQL queries?
Correct answer
A. Using the django.db.connection object
15 views
A. Using the django.db.connection object
15 views
Raw SQL can be run using the django.db.connection object: open a cursor with connection.cursor(), call execute() and fetch the rows.
The alternative is Model.objects.raw(), which returns model instances. Always pass parameters separately to avoid SQL injection.
For more solved questions like this one, see Computer Science MCQs on MCQsExam.com.
Not sure why the answer is A? Ask here and get an explanation.