Last active
December 26, 2021 18:26
-
-
Save hossainchisty/2092b63e76438fe2820fd36f8f7d3f13 to your computer and use it in GitHub Desktop.
How to get the total sales of via month in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Note: Let's say we have Sale model total is a field to hold sales amount then we can do like that... we find month via created_at which is DateTimeField. | |
sales = Sale.objects.all() | |
months = sales.datetimes('created_at', kind="month") | |
for month in months: | |
month_sale = sales.filter(created_at__month=month.month) | |
month_total = month_sale.aggregate(Sum('total'))['total__sum'] | |
print(f'Month: {month.strftime("%B")} - Total Sale: {month_total}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment