gpt4 book ai didi

python - Django - 用不常见的字段数据压缩多个查询集

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:10 25 4
gpt4 key购买 nike

我有一个模型,其中我获取了published countunder process countrejected countreceived count按月

class PreData(models.Model):

status=models.CharField(max_length=200,default=None,null=True)

receivedon=models.DateField(default=None,blank=False,null=True)

publishedon = models.DateField(default=None, blank=True, null=True)

received count 是基于模型中 receivedon DateField 的每月计数,published count 是基于每月模型中 publishedon DateField 的计数,rejected countunder process count 是基于特定 的计数status 模型中 CharField 的值。

我在写完下面的查询后苦苦挣扎,我对如何获取数据来填充列一无所知(见图)。我不确定我写的查询是否有帮助。

当我想压缩 received_monthly_datapublished_monthly_datareceived_monthly_data 有四月份的数据时,问题就来了 published_monthly_data 没有四月份。当我压缩时,结果将在 4 月份公布。我不知道该怎么做。

received_monthly_data = PreData.objects.filter(journaluser=request.user.username).\
annotate(month=TruncMonth('receivedon'),year=TruncYear('receivedon')).values('month','year').\
annotate(c=Count('id')).order_by('-month')

published_monthly_data = PreData.objects.filter(Q(journaluser=request.user.username)&~Q(pdfsenton=None)). \
annotate(month=TruncMonth('publishedon'), year=TruncYear('publishedon')).values('month', 'year'). \
annotate(c=Count('id')).order_by('-month')


underproc_data= PreData.objects.filter(Q(journaluser=request.user.username)&~Q(status="[Published]"))

I need the data to fill these columns

非常感谢任何帮助。

最佳答案

也许您可以使用 itertools.zip_longest 来压缩最长的序列,并用 None 替换较短序列中的缺失值。这样您就不会丢失四月份的数据。

如果您希望使用 None 以外的值,请指定 fillvalue 参数。

来自 https://docs.python.org/3/library/itertools.html#itertools.zip_longest

itertools.zip_longest(*iterables, fillvalue=None)

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

请注意,该函数在 Python 2 中称为 itertools.izip_longest

关于python - Django - 用不常见的字段数据压缩多个查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com