Trying to understand how sql query is generated by django framework?

I am using django debugger in pycharm to see how ORM does the magic. I have a simple model and a very basic query. Model.objects.get(id=1). The first step in click (debugger) takes me to django.db.models.manager.py. The get method of ManagerDescriptor class in this module is called. Then the pointer goes to django.db.models.query and as soon as the init method is complete the query is generated out of nowhere. Where did the query select Model.name, Model.age ... from Model come from?

I am sure I am missing something here, if anyone knows the internal working or can direct me to any tutorials on this please guide me. Thank you

Model.objects.get(id=1)

Django generates SQL query for ORM Querset not a single object I mean you can print SQL query for .filter() or .all() not for .get() like this...

NOTE - Here .get() method return a single object not return Queryset.

obj = Model.objects.filter(id=1)
print(obj.query)
Back to Top