My model is:
class Test():
date1 = models.DateTimeField()
date2 = models.DateTimeField()
I can find out objects who's date2
is greater than date1
, using the following query:
Test.obejcts.filter(date2__gt=F('date1'))
I would like to find all the objects who's date2
is greater than date1
by one year.
How can I find out objects based on difference between date1
and date2
?
General Solution:
You can
annotate
the date difference and then check this against thetimedelta(days=365)
(pretty close to what @Anonymous suggests in his comment):PostgreSQL Specific Solution:
If you are using
PostgreSQL
, there is another option derived from this answer: