python - Django TruncWeek 中的错误返回
问题描述
TruncDate 和 TruncMonth 返回正确的数据,但是当使用 TruncWeek 时,数据不会像我使用 TruncMonth/Date 那样返回
使用 TruncDate 和 TruncMonth 的两个函数模型运行良好,并且 TruncWeek 必须运行
class TblogByDateSerializer(serializers.ModelSerializer):
groupby = serializers.CharField()
total = serializers.IntegerField()
class Meta:
model = Tblog
fields = ('groupby','total')
查询集:
Tblog.objects.filter(status='success').annotate(groupby=TruncMonth('createtime',output_field=DateField())).values('groupby').annotate(total=Count('createtime')).order_by('-groupby')[:7]
如果我将TruncMonth
函数更改TruncWeek
为代码应该按周分组,
当我使用使用 TruncMonth和使用 TruncDate时的结果这是使用 TruncWeek时的问题
使用 TruncMonth 和 TruncDate 时的结果,TruncWeek 也应该像这样返回,
"results": [
{
"groupby": "2019-08-01",
"total": 77
},
{
"groupby": "2019-07-01",
"total": 24
}
"results": [
{
"groupby": "2019-08-06",
"total": 29
},
{
"groupby": "2019-08-05",
"total": 6
},
{
"groupby": "2019-08-04",
"total": 4
},
{
"groupby": "2019-08-03",
"total": 4
},
{
使用 TruncWeek 时
"results": [
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
},
{
"groupby": "2019-08-05",
"total": 1
}
解决方案
我改变了TruncWeek('createtime',output_field=DateField())
它.TruncWeek('createtime')
,它对我有用,
我认为问题在于使用groupby = serializers.CharField()
和outpufield using DateField
推荐阅读
- r - R Data.Table 重新编码许多列
- javascript - 无法删除 onmouseout 上的课程
- java - 服务器断开连接或使用 OPC UA milo 重新启动后如何处理重新订阅?
- apache-spark - Fillna 特定列和特定行的值
- reactjs - 用布局反应嵌套路由
- python - 随机打乱值相同的查询结果
- javascript - Javascript SumOfTwo
- javascript - 将变量连接为函数内的输入名称
- codesandbox - 无法让 CSS 文件在 CodeSandbox 中工作
- php - Windows 上带有 xampp 1.5.5 和带有 apache 的 php 4.5 的本地网站。一些操作后,它不再工作了