1 | initial version |
To print the Django SQL query related to an aggregation, follow these steps:
from django.db import connection
from django.db.models import Count, Avg
qs = MyModel.objects.values('field1').annotate(num=Count('id'), avg=Avg('field2'))
query
attribute of the queryset object to get the SQL query:print(qs.query)
result = qs.all()