django报表系统_使用Django中的在线摘要生成列表/报表

I am trying to write a view that will generate a report which displays all Items within my Inventory system, and provide summaries at a certain point. This report is purely just an HTML template by the way.

In my case, each Item is part of an Order. An Order can have several items, and I want to be able to display SUM based summaries after the end of each order.

So the report kind of looks like this:

Order #25

Some Item 2 1 29.99 29.99

Another Item 4 0 10.00 40.00

6 1 39.99 69.99

Order #26

... Etc, you get the point

Now, I'm perfectly capable of displaying all the values and already have a report showing all the Items, but I have no idea how I can place Subtotals within the report like that without doing alot of queries. The Quantity, Qty Sold, and Cost fields are just part of the Item model, and Cost Value is just a simple model function.

Any help would be appreciated. Thanks in advance :-)

解决方案

Subtotals are SELECT SUM(qty) GROUP BY order_number things.

They are entirely separate from a query to get details.

The results of the two queries need to be interleaved. A good way to do this is to create each order as a tuple ( list_of_details, appropriate summary ).

Then the display is easy

{% for order in orderList %}

{% for line in order.0 %}

{{ line }}

{% endfor %}

{{ order.1 }}

{% endfor %}

The hard part is interleaving the two queries.

details = Line.objects.all()

ddict = defaultdict( list )

for d in details:

ddict[d.order_number].append(d)

interleaved= []

subtotals = ... Django query to get subtotals ...

for s in subtotals:

interleaved.append( ( ddict[s.order], s.totals ) )

This interleaved object can be given to your template for rendering.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值