Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django keyword can't be an expression

I am trying to create a query where I get a filtered list.

The model that I am calling the query on is:

class WorkoutRecord(models.Model):

    challenge = models.ForeignKey(Challenge)

    workout_email_sent = models.DateTimeField(default=datetime.now)

    status = models.IntegerField(default=1) 

My query is:

L = WorkoutRecord.objects.filter(status = 1, challenge.user = request.user, workout_email_sent__lt = today)

The error message I am getting is:

keyword can't be an expression

I know the issue is on the challenge.user = request.user part of the query but I'm not sure how to write the query so it filters on this ForeignKey properly.

like image 936
bbrooke Avatar asked Jun 07 '13 22:06

bbrooke


1 Answers

Use __ to follow foreign key relationships in a queryset:

L = WorkoutRecord.objects.filter(status=1, challenge__user=request.user, workout_email_sent__lt=today)
like image 88
Peter DeGlopper Avatar answered Nov 12 '22 15:11

Peter DeGlopper