See raw SQL query

@SQL @query

Sometimes we want to see the raw SQL query performed by our ORM. This looks something like:

SELECT "routes_route"."id", "routes_route"."rname", "routes_route"."rdescription", "routes_route"."ropener", "routes_route"."rcrag_id", "routes_route"."grade_id", "routes_route"."length", "routes_route"."base_unit", "routes_route"."rtype", "routes_route"."numpitch", "routes_route"."avg_rating" FROM "routes_route" INNER JOIN "crags_crag" ON ("routes_route"."rcrag_id" = "crags_crag"."id") WHERE ("crags_crag"."city_id" = 3 AND ("crags_crag"."toprope" = Some routes OR "crags_crag"."toprope" = All routes))

To do this, we simply use the query attribute of a queryset (an object returned when we use objects.get() or objects.filter()

So for example to print this query to console we would do:

print(Some.objects.filter(pk=3).query)