Get raw SQL query for further execution in database client

84 Views Asked by At

How am I able to retrieve the produced plain SQL statement from a Django ORM query which I can use in another database client program?

For example, having a ModelA.objects.filter(name='abc') queryset. I imagine setting a breakpoint somewhere in the Django/3rd-party-apps code to get a plain Select * from TabelModelA where name='abc'; statement which I can directly copy/paste into DBeaver or something else and execute without the need for further processing into the needed SQL dialect.

We use Server SQL and the django-mssql package.

I know the django.db.connection.queries thing and its resulting SQL/params strings, but that's not what I mean. Is there something else? Or is it doable in pyodbc? Or is there another technique?

1

There are 1 best solutions below

3
0urz4g On

It's not very clear what you want to do.

But for getting SQL queries made by Django ORM, these two projects are quite usefull :

https://github.com/robhudson/django-debug-toolbar

https://github.com/jazzband/django-silk

or natively :

qs = ModelA.objects.filter(name='abc')
print(qs.query)