Django ORM 从日期时间检索月份名称

Django ORM to retrieve month name from date time

我需要检索月份名称和相应月份的总价。 这是我的订单 table 样品

我的尝试是:

month_wise_sales = Order.objects\
.annotate(month=ExtractMonth('created_at'))\
.values('month')\
.annotate(total_sales=Sum('total_price'))

会return

<QuerySet [{'month': 7, 'total_sales': Decimal('138400.00')}, {'month': 9, 'total_sales': Decimal('1150.00')}]>

我的愿望是这样的

[{'month': July, 'total_sales': Decimal('138400.00')}, {'month': September, 'total_sales': Decimal('1150.00')}]

如何解决?

这对我来说很好用。 工作查询[=​​12=]

month_wise_sales = Order.objects\
.annotate(month=Cast('created_at', DateField()))\
.values('month')\
.annotate(total_sales=Sum('total_price'))

然后在模板中使用这个

{{ monthly_sales.month | date:"M"}}

通过这个,我将得到总价与月份名称的总和。