Which among the below is the most efficient method to get the *recent 
transactions*?

*1##using subquery*
transactions = list(
            Transaction.objects.filter(
                pk__in=Subquery(
                    Transaction.objects.filter(user=self.request.user)
                    .order_by("-merchant", "-date_of_payment")
                    .distinct("merchant")
                    .values("pk")
                 )
        ).order_by("-date_of_payment")

*2##using one query and sorting*         
transactions = (
            Transaction.objects.filter(user=self.request.user)
            .order_by("-merchant", "-date_of_payment")
            .distinct("merchant")
        )
recent_transactions = sorted(
            transactions, key=operator.attrgetter("date_of_payment"), 
reverse=True
        )
       
*3##using two queries*
transactions = 
Transaction.objects.filter(user=self.request.user).order_by("-merchant", 
"-date_of_payment").distinct("merchant")
recent_transactions = 
Transaction.objects.filter(id__in=transactions).order_by('-date_of_payment')

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/43a65961-4998-4520-b73f-1ed1cba14ae8n%40googlegroups.com.

Reply via email to