I have a model representing a transaction between two users, like this:
class Transaction(models.Model):
buyer = models.ForeignKey(
Person, on_delete=models.SET_NULL, null=True, related_name="bought"
)
seller = models.ForeignKey(
Person, on_delete=models.SET_NULL, null=True, related_name="sold"
)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
I would like to get the number of transactions for each user (either as a buyer or a seller). If a I want to count on only one field, I can just do :
Transaction.objects.values('seller').annotate(Count('seller'))
but I can't manage to do it on two fields at the same time in 1 query. Is there a way to do that ?
Thanks