Given:
from django.conf import settings
from django.db import models
class Membership(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
started = models.DateTimeField()
num_weeks = models.IntegerField()
How can I get a QuerySet of all memberships that haven't expired?
I want to write something like this...
from datetime import datetime, timedelta
Membership.objects.annotate(
ended=F('started') + datetime.timedelta(weeks=F('weeks'))
).filter(ended__gt=datetime.today())
But I doubt the F expressions are compatible with timedeltas, and even if they were, timedelta(weeks=) is not compatible with F expressions, so this code fails. I'm guessing I need to convert the IntegerField to some type of database-level duration type but I can't find a clue as to how to do such a thing in the docs.