I'm trying to fetch all expired objects for a model in my Django application.
The model looks like this:
MyModel(models.Model):
owner = models.ForeignKey(User)
last_check = models.DateTimeField(blank=True, null=True)
interval = models.IntegerField(default=1800)
I need to fetch all matching objects where last_check is earlier than now minus the check interval in order to determine if the object should be checked again.
No problem if I use a static interval:
time_diff = datetime.now() - timedelta(seconds=1800)
MyModel.object.filter(last_check__lte=time_diff)
But when the interval is on the model itself I can't figure out how to do it. This is what I tried:
objects_to_check = MyModel.objects.filter(
last_check__isnull=False
).filter(
last_check__lte=datetime.now() - timedelta(seconds=F('interval'))
)
But that didn't work at all, only gave me the following error
unsupported type for timedelta seconds component: F
Any idea how to solve this?