I have for example a model like this:
class Transaction(models.Model):
amount = models.FloatField()
seller = models.ForeignKey(User, related_name='sells', on_delete=models.CASCADE)
buyer = models.ForeignKey(User, related_name='purchased', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
I want to group by all transactions seperated by each date and find some of them. If the created_at
is of type DateField
I could simply find the count on each day by this query:
Transaction.objects..values('created_at').annotate(count=Count('created_at_date')
But this doesn't work DateTimeFeilds
.
My question is how can i find total count of transactions for each date for this model type.
The following will work for django 2.1 and above.
Transaction.objects.values('created_at__date').annotate(count=Count('id')).values('created_at__date', 'count').order_by('created_at__date')
You need to use __date
after the name for lookup and need to put an order_by
at the end.
Not sure about the previous django versions.