[Answered ]-Assign table alias name in a QuerySet


You can see the sql query to do the next:

queryset = Price.objects.all()
print queryset.query

If you know first sql query. You can do the subquery better.

Although, I do the next:

price_max = Price.objects.all().order_by('-created')[0]
queryset = Price.objects.filter(created=price_max)

Or the best:

from django.db.models import Max
price_max = Price.objects.aggregate((Max('created'))['created__max']
queryset = Price.objects.filter(created=price_max)

Leave a comment