比较日期的年和月字段大于 [英] compare year and month of date field to be greater than

查看:48
本文介绍了比较日期的年和月字段大于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行以下查询:

today = datetime.date.today()
year=today.year
month=today.month
news=News.objects.filter(date__year__lt = year,date__month__lt=month)

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

Note:News object has a field named date but I get this error:

Join on field 'date' not permitted. Did you misspell 'year' for the lookup type?

你有什么想法?

预先感谢

推荐答案

您不能将 __ lt 附加到 __ year __ month 上.只有最后一个双下划线的位被认为是限定符,它被视为遍历之前的所有内容,即Django将尝试在名为 date year 的字段>,这显然是不正确的.

You can't append __lt onto to __year or __month. Only the last double-underscored bit is consider the qualifier, everything before it is treated as a traversal, i.e. Django will try to look up a field named year on join table named date, which is obviously not correct.

对于这样的事情,您只需要直接比较日期即可:

For something like this you'll need to just compare the date directly:

date = datetime.date(year, month, 1)
news = News.objects.filter(date__lt=date)

这篇关于比较日期的年和月字段大于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆