我有一本字典
context['income'] = Income.objects.filter(assigned_to__username=self.request.user). \
filter(status=Income.STATUS_PENDING). \
annotate(month=ExtractMonth('created')).values('month'). \
annotate(value=Coalesce(Sum('value'), 0)).values('month', 'value')每次都会有这样的结果:
<QuerySet [{'month': 9, 'value': Decimal('12.50')}]>取而代之的是我想用文字显示月份的月份号:1-1月,2-2月.9-9月等。我怎么做呢?
我想在一个图表中使用这个,在这个图表中,每个月都会显示总价值。
template.html
<script type="text/javascript">
$(document).ready(function () {
Morris.Bar({
element: 'Income',
data: [
{% for item in income %}
{ Month: '{{ item.month }}', Income: '{{ item.value }}' },
{% endfor %}
],
xkey: 'Month',
ykeys: ['Income'],
labels: ['Euro'],
resize: true,
lineColors: ['#33414E', '#95B75D']
});
});
</script>发布于 2017-09-07 15:01:15
在JS中进行简单的数组查找是最简单的方法。
$(document).ready(function () {
monthNames = ['January', 'February', 'March', 'April', ...];
Morris.Bar({
element: 'Income',
data: [
{% for item in income %}
{ Month: monthNames[{{ item.month }} - 1], Income: '{{ item.value }}' },
{% endfor %}
],发布于 2017-09-07 14:51:55
您可以尝试使用模板-日期
{ Month: '{{ item.month|date:"F" }}', Income: '{{ item.value }}' },发布于 2017-09-07 14:57:53
Income.objects.filter(assigned_to__username=self.request.user).\
extra(select={'month': "MONTHNAME(created)"}).\
values('month')https://stackoverflow.com/questions/46099295
复制相似问题