从ORM中提取数据并按日期分组

时间:2019-07-01 12:14:47

标签: python django django-models orm

我有一个函数,其数据如下:

my_workouts = Workouts.objects.groupby('my_date').filter(
    my_date__year=year, my_date__month=month)

我想对另一个按日期分组权重的模型做同样的事情:

Models.py

class Quiz(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='quizzes')
    scheduled_date = models.DateTimeField(max_length=255, default=0)
    weight = models.IntegerField(default=0)

我尝试过的事情:

data = Quiz.objects.orderby('scheduled_date').
       filter(owner_id=request.user.pk,my_date__year=year, my_date__month=month).
       annotate(c=Sum('weight')).values('c')

但这不起作用,请帮助!

1 个答案:

答案 0 :(得分:1)

检查语法

orderby 必须为 order_by

data = Quiz.objects.order_by('scheduled_date').
       filter(owner_id=request.user.pk,my_date__year=year, my_date__month=month).
       annotate(c=Sum('weight')).values('c')

推荐this