Python Django过滤器查询按时间增量设置

Python Django过滤器查询按时间增量设置,python,django,Python,Django,有什么好方法可以过滤响应晚于(例如)500秒的设备 因此,假设我的模型: class Device(models.Model): last_response = models.DateTimeField(null=True, blank=True) 我的最佳举措是: from django.utils import timezone for d in Device.objects.all(): now = timezone.now() if d.last_respon

有什么好方法可以过滤响应晚于(例如)500秒的设备

因此,假设我的模型:

class Device(models.Model):
    last_response = models.DateTimeField(null=True, blank=True)
我的最佳举措是:

from django.utils import timezone

for d in Device.objects.all():
    now = timezone.now()
    if d.last_response and (now - d.last_response).seconds < 500:
        # Do something
从django.utils导入时区
对于设备.objects.all()中的d:
now=时区。now()
如果d.last_响应和(现在-d.last_响应)。秒<500:
#做点什么
但我不想查询所有的数据库。如何使其与筛选器一起工作,例如Device.objects.filter(…某些参数…)中d的

from django.utils import timezone
from datetime import timedelta
Device.objects.filter(last_response__lte=timezone.now()-timedelta(seconds=500))