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.
我知道问题出在challenge.user = request.user查询的一部分,但我不确定如何编写查询,以便正确过滤这个ForeignKey。
1 个解决方案
#1
21
Use __ to follow foreign key relationships in a queryset:
使用__跟踪查询集中的外键关系:
L = WorkoutRecord.objects.filter(status=1, challenge__user=request.user, workout_email_sent__lt=today)
#1
21
Use __ to follow foreign key relationships in a queryset:
使用__跟踪查询集中的外键关系:
L = WorkoutRecord.objects.filter(status=1, challenge__user=request.user, workout_email_sent__lt=today)