比较日期和月份的日期字段大于

Asm*_*ari 4 python django

我打算做这个查询:

today = datetime.date.today()
year=today.year
month=today.month
news=News.objects.filter(date__year__lt = year,date__month__lt=month)
Run Code Online (Sandbox Code Playgroud)

注意:新闻对象有一个名为date 但是我收到此错误的字段:

Join on field 'date' not permitted. Did you misspell 'year' for the lookup type?
Run Code Online (Sandbox Code Playgroud)

你有什么想法?

提前致谢

Chr*_*att 10

你无法附加__lt__year__month.只有最后一个双下划线位考虑限定符,它之前的所有内容都被视为遍历,即Django将尝试查找year在连接表上命名的字段date,这显然是不正确的.

对于这样的事情,你需要直接比较日期:

date = datetime.date(year, month, 1)
news = News.objects.filter(date__lt=date)
Run Code Online (Sandbox Code Playgroud)