Django: Using F arguments in datetime.timedelta inside a query

Just avoid timedelta‘s F-ignorance

filter knows about F, but timedelta does not.
The trick is to keep the F out of the timedelta argument list:

ThatModel.objects.filter(
    last_datetime__lte=now + datetime.timedelta(seconds=1)*F("interval"))

This will work with PostgreSQL, but, alas, not with SQlite.

Leave a Comment