Skip to content Skip to sidebar Skip to footer

Compare Year And Month Of Date Field To Be Greater Than

I'm going to do this query: today = datetime.date.today() year=today.year month=today.month news=News.objects.filter(date__year__lt = year,date__month__lt=month) Note:News object

Solution 1:

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)

Solution 2:

Django has trouble with certain lookups when working through a relation (e.g. date__year__*). I think this is something they are working on for future versions.

Does this produce an acceptable result?

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

Post a Comment for "Compare Year And Month Of Date Field To Be Greater Than"