这是我的模特
class UserAward(models.Model):
user = models.ForeignKey(User, related_name='awards')
award = models.CharField(max_length=255)
week = models.SmallIntegerField(default=0)
year = models.IntegerField(default=0)
points_gained = models.IntegerField(default=0)
timestamp = models.DateTimeField(auto_now=True)
当我这样说时:
users = User.objects.filter(awards__award='level up', awards__week=prev_wk, awards__year=prev_year).order_by('-profiles__level')
我收到此错误:
Related Field has invalid lookup: year
如果我删除filter(awards__year=prev_year)
查询工作正常。
问题出在哪里?
答案 0 :(得分:3)
试试这个:
users = User.objects.filter(awards__award='level up').filter(awards__week=prev_wk).filter(awards__year__exact=prev_year).order_by('-profiles__level')
基本上django使用__year
从日期对象中提取年份字段,它认为你想从该字段中获取年份