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?
Hm… I am not sure if you can modify the value of the field being looked up. Database would still need to retrieve the value first, apply the function timedelta on it and than do a comparison.
I think you might need to rethink your architecture… but I might be wrong. Maybe instead of storing last_check, store the expired property, that you can check directly:
and then when updating the model:
or something like that.