我有一个模型,其中我获取了published count,under process count,rejected count,received 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 count 和 under process count 是基于特定 的计数status 模型中 CharField 的值。
我在写完下面的查询后苦苦挣扎,我对如何获取数据来填充列一无所知(见图)。我不确定我写的查询是否有帮助。
当我想压缩 received_monthly_data 和 published_monthly_data 但 received_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。
