使用Django中的在线摘要生成列表/报表 [英] Generating lists/reports with in-line summaries in Django

查看:1032
本文介绍了使用Django中的在线摘要生成列表/报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个视图,生成一个报告,显示我的库存系统中的所有项目,并在某一点提供摘要。这份报告完全只是一个HTML模板。



在我的情况下,每个项目都是订单的一部分。一个订单可以有几个项目,我希望能够在每个订单结束后显示基于SUM的摘要。



所以报告类型如下所示: / p>

 订单#25<数量> <已出售数量> <成本> <成本值> 
一些项目2 1 29.99 29.99
另一个项目4 0 10.00 40.00
<小计行> 6 1 39.99 69.99
订单#26<数量> <已出售数量> <成本> <成本值>
...等等,你得到点

现在,我完全有能力显示所有值,并且已经显示了显示所有项目的报告,但是我不知道如何在没有执行 alot 查询的情况下,在报表中放置小计。数量,数量出售和成本字段只是项目模型的一部分,成本价值只是一个简单的模型函数。



任何帮助将不胜感激。感谢提前: - )

解决方案

小计是 SELECT SUM(qty)GROUP BY order_number 东西。



它们与查询完全不同,以获取详细信息。



这两个查询需要交错。一个很好的方式是创建一个元组元素(list_of_details,适当的摘要)



然后显示很简单

  {%在orderList%中订单} 
{%for order in order.0%}
{{line}}
{%endfor%}
{{order.1}}
{%endfor%}
/ pre>

困难的部分是交叉两个查询。

 详细信息= Line.objects.all()
ddict = defaultdict(list)
详细信息:
ddict [d.order_number] .append(d)

interleaved = []
subtotals = ... Django查询以获取小计...
在小计中的s:
interleaved.append((ddict [s.order],s.totals ))

可以给出这个交错的对象到您的模板进行渲染。


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        <Qty> <Qty Sold> <Cost> <Cost Value>
Some Item          2       1       29.99    29.99
Another Item       4       0       10.00    40.00
<Subtotal Line>    6       1       39.99    69.99
Order #26        <Qty> <Qty Sold> <Cost> <Cost Value>
... 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.

这篇关于使用Django中的在线摘要生成列表/报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆