xadmin没有changelist_view,可以用
object_list_template指定模板
-
class CostAdmin(object):
-
list_display = ("id", "idcname", "costnum", "content", "createdate")
-
list_per_page = 10
-
object_list_template = 'change_list.html' #自义模板
-
-
-
#自定义模板 接参过滤数据
-
def get_list_queryset(self):
-
this_year = self.request.GET.get("year")
-
if not this_year:
-
queryset = super(CostAdmin, self).get_list_queryset()
-
return queryset
-
else:
-
queryset = super(CostAdmin, self).get_list_queryset()
-
return queryset.filter(createdate__year=this_year)
-
-
#添加数据 https://www.cnblogs.com/roystime/p/6875709.html
-
def get_context(self):
-
#读取所需要的数据
-
#context = CommAdminView.get_context(self)
-
context = super().get_context()
-
this_year = self.request.GET.get("year")
-
if not this_year:
-
this_year = time.strftime("%Y", time.localtime(time.time()))
-
year_data = (
-
models.Cost.objects.filter(createdate__year=this_year).extra(select={"year":"date_format(createdate,'%%Y')"}).values("year","idcname").annotate(sum_costnum=Sum("costnum")).values("year","idcname__name","sum_costnum").order_by()
-
)
-
。。。。。
-
。。。。。
-
-
context.update({"this_year":this_year})
-
context.update({"link_res":link_res})
-
context.update({"year_data":year_data})
-
context.update({"month_data":month_data})
-
context.update({"legend":list(dict1.keys())})
-
context.update({"axis":axis})
-
context.update({"result":result})
-
-
return context
-
-
xadmin.site.register(models.Cost, CostAdmin)
模板可以引入templates/change_list.html
-
{% extends "xadmin/views/model_list.html" %}
-
{% load static %}
-
-
{% block extrahead %}
-
<script type="text/javascript" src="/static/echarts/5.3.3/echarts.min.js"></script>
-
-
{% endblock %}
-
-
{% block content %}
-
-
<h4>{{this_year}}年度汇总</h4>
-
<table class="table table-bordered table-striped table-hover">
-
<thead>
-
<tr>
-
<th>年度</th>
-
<th>供应商</th>
-
<th>金额</th>
-
</tr>
-
</thead>
-
<tbody>
-
{% for i in year_data %}
-
<tr>
-
<td>{{i.year}}</td>
-
<td>{{i.idcname__name}}</td>
-
<td>{{i.sum_costnum}}</td>
-
</tr>
-
{% endfor %}
-
</tbody>
-
</table>
-
<hr>
-
{% if link_res %}
-
{% for i in link_res %}
-
<a href="?year={{i.0}}"><font size="5">{{i.1}}</font></a>
-
{% endfor %}
-
{% endif %}
-
<hr>
-
-
{{ block.super }}
-
<hr>
-
<div id="main" style="width:100%;height:450px;"></div>
-
<script type="text/javascript">
-
// 基于准备好的dom,初始化echarts实例
-
var myChart = echarts.init(document.getElementById('main'));
-
-
// 指定图表的配置项和数据
-
var option = {
-
title: {
-
text: '每月统计'
-
},
-
tooltip: {},
-
label: {
-
show: false,
-
},
-
legend: {
-
data:{{ legend|safe }},
-
right: '4%',
-
},
-
xAxis: {
-
data: {{ axis|safe }}
-
},
-
yAxis: {
-
type: 'log',
-
min:1,
-
logBase:10
-
},
-
series: {{ result|safe }}
-
};
-
-
// 使用刚指定的配置项和数据显示图表。
-
myChart.setOption(option);
-
-
</script>
-
-
-
{% endblock %}
阅读(343) | 评论(0) | 转发(0) |