General Solution:
You can annotate
the date difference and then check this against the timedelta(days=365)
(pretty close to what @Anonymous suggests in his comment):
Test.objects.annotate(
duration=F('date2') - F('date1')
).filter(duration__gt=timedelta(days=365))
PostgreSQL Specific Solution:
If you are using PostgreSQL
, there is another option derived from this answer:
from django.db.models import F, Func
Test.objects.annotate(
duration = Func(F('date2'), F('date1'), function='age')
).filter(duration__gt=timedelta(days=365))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…