[Solved]-Django filter on date difference between columns

22👍

This would do the magic:

from django.db.models import DurationField, F, ExpressionWrapper
import datetime

Race.objects.annotate(
    diff=ExpressionWrapper(F('end') - F('start'), output_field=DurationField())
).filter(diff__gte=datetime.timedelta(5))

This will return all Race instances whose duration is greater than or equal to 5

References:

👤JPG

Leave a comment