我刚开始使用 django。我的模型非常简单,由时间戳和值(温度,每分钟更新)组成。我想检索过去 7 天中每一天的最大值。
我需要查询7次还是有“捷径”?
您可以利用annotate() https://docs.djangoproject.com/en/dev/topics/db/aggregation/ and extra() https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.extra:
start_date = date.today() - timedelta(days=7)
MyModel.objects.filter(timestamp__gte=start_date).extra(select={'day': connection.ops.date_trunc_sql('day', 'timestamp')}).values('day').annotate(max_temperature=Max('temperature'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)