Compare year and month dates to be more than

I am going to make this request:

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

Note. The News object has a field called date but I get this error:

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

what's your idea

early

+4
source share
2 answers

You cannot add __lt to __year or __month . Only the last double underscored bit considers the qualifier, all before it is considered a bypass, that is, Django will try to find a field named year in the connection table named date , which is obviously incorrect.

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

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

Django has problems with certain searches when working through a relation (for example, date__year__* ). I think this is what they are working on in future versions.

Does this get an acceptable result?

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

Source: https://habr.com/ru/post/1396236/


All Articles