chirag
chirag

Reputation: 173

Extract data from ORM and group by Date

I have a function which data like this :

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

I want to do the same with another model where I want to group the weight by date:

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)

What I tried:

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')

But this is not working, Please Help!

Upvotes: 1

Views: 107

Answers (1)

rahul.m
rahul.m

Reputation: 5854

check your syntax

orderby must be 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')

refer this

Upvotes: 1

Related Questions