简体   繁体   English

Django - 分组后如何组合两个查询

[英]Django - How to combine two queries after doing a group by

GOAL: Group by ITEM all INCOMING and its OUTGOING then SUM the quantity to come up with a simple table in my template like this:目标:按 ITEM 所有 INCOMING 及其 OUTGOING 分组,然后对数量求和,以在我的模板中得出一个简单的表格,如下所示:

Item        | Total In | Total Out| Stock
1. Cement   | 400      | 300      | 100

My models.py我的模型.py

class Incoming(models.Model):
    ...
    project_site = models.ForeignKey(ProjectSite, related_name='in_project_site', default=7, null=True, on_delete = models.SET_NULL)
    item = models.ForeignKey(Item, related_name='item_in', null=True, on_delete = models.SET_NULL)
    quantity = models.DecimalField('Quantity', db_index=True, max_digits=20, decimal_places=2, default=0)
    ...

class Outgoing(models.Model):
    ...
    base_in = models.ForeignKey('warehouse.Incoming', related_name='out', on_delete = models.SET_NULL, null=True)
    quantity = models.DecimalField('Quantity', db_index=True, max_digits=20, decimal_places=2, default=0)
    ...

Okay so the goal is to group all incoming by ITEM, then annotate the current total Incoming and Outgoing.好的,所以目标是按 ITEM 对所有传入进行分组,然后注释当前的总传入和传出。 So naturally I did this in my views.py所以我很自然地在我的views.py中这样做了

 incoming = Incoming.objects.filter(project_site__id=self.object.id)

 context['inventory'] =  incoming.values('item__item_name')\
    .annotate(tot_in=Sum('quantity'))\
    .annotate(tot_out=Sum('out__quantity'))

This does not give the proper SUM for the total_in, now after some search this is apparently an issue.这并没有为 total_in 提供正确的 SUM,现在经过一番搜索,这显然是一个问题。 One suggestion I found was to separate the query into to so this is what I did.我发现的一个建议是将查询分开,所以这就是我所做的。

context['current_in'] =  incoming.values('item__item_name').annotate(tot_in=Sum('quantity'))
context['current_out'] =  incoming.values('item__item_name').annotate(tot_out=Sum('out__quantity'))

This gives me the correct SUM for each In and Out, now my problem is how to UNITE/COMBINE the two again so I could easily loop through it on my template?这为每个输入和输出提供了正确的 SUM,现在我的问题是如何再次 UNITE/COMBINE 两者,以便我可以轻松地在我的模板上循环遍历它? I did a search and tried the following to no luck.我进行了搜索并尝试了以下方法,但没有成功。

list(chain(pagelist1, pagelist2))
# This does not combine items with same ITEM name. The output for this is
Item        | Total In | Total Out| Stock
Cement      | 400      |          | 
Cement      |          | 300      | 

doing a UNION doesn't help either.做一个 UNION 也无济于事。 Any help would do.任何帮助都可以。

How about this:这个怎么样:

incoming.values('item__item_name').annotate(tot_in=Sum('quantity'), tot_out=Sum('out__quantity'))

If it does not work then but you can use pythonic approach here using zip_longest :如果它不起作用,那么你可以在这里使用 pythonic 方法使用zip_longest

from itertools import zip_longest

current_in = incoming.values('item__item_name').annotate(tot_in=Sum('quantity'))
current_out = incoming.values('item__item_name').annotate(tot_out=Sum('out__quantity'))
context['current'] = [{**u, **v} for u, v in zip_longest(current_in, current_out, fillvalue={})]   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM