I'm trying to find all the ORM objects updated in the last 30 seconds. The standard way to do this would be:
reftime = timezone.now() - relativedelta(seconds=30)
queryset = MyModel.objects.filter(updated_at__gte=reftime)
This works so long as all the application servers running the django app have their clocks synchronized, which sounds easy but in practice really isn't. Clock skew is real.
Fortunately the database server has a single clock which we should be able to rely on as the definitive clock for this kind of thing. And django has this nice object called Now() in django.db.models.functions which seems like it should be helpful for this kind of thing. But I've never seen any code sample for this method that does any math to shift the time by something like 30 seconds. e.g. this does NOT WORK:
reftime = django.db.models.functions.Now() - relativedelta(seconds=30)
How can I do a django ORM query using a timestamp relative to the SQL server's clock?