有什么好的方法可以过滤掉响应时间超过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
但是我不想为此查询整个数据库。我该如何使用过滤器使其工作,例如for d in Device.objects.filter(..一些参数..):
?