python - django request with two date field -


i trying attempt request filter on combination of date, , integer represent year, , encounter trouble doing it.

here model :

class exemple(models.model):     date_field = models.datefield()     year_field = models.integerfield() 

here request work :

exs = exemple.objects.extra(where=["date(date_field, '+' || year_field|| ' years') < date('now') or date_field null"]) 

i want exact same request using django orm, tried (which didn't work)

from django.db.models import f import datetime exs = exemple.objects.filter(date_field__lt=datetime.timedelta(days=-365*f('year_field'))+datetime.datetime.today().year) 

i think problem f() isn't yet value @ moment timedelta called

thanks advance solution, or idea might help

that's problem. can't complex calculations orm. however, might able take advantage of __year lookup simplify math:

exemple.objects.filter(date_field__year__lt=f('year_field')) 

anything more complex that, though, , have use extra.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -