12

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.

4

1 回答 1

30

使用 __ 跟踪查询集中的外键关系:

L = WorkoutRecord.objects.filter(status=1, challenge__user=request.user, workout_email_sent__lt=today)
于 2013-06-07T22:52:42.373 回答