Django filter queryset by timedelta
<p>What could be nice way to filter devices which response was later than, for example, 500 seconds?</p>

<p>So assume my model:</p>

<pre><code>class Device(models.Model):
    last_response = models.DateTimeField(null=True, blank=True)
</code></pre>

<p>My best move was:</p>

<pre><code>from django.utils import timezone

for d in Device.objects.all():
    now = timezone.now()
    if d.last_response and (now - d.last_response).seconds &lt; 500:
        # Do something
</code></pre>

<p>But I don't want to query all database for this. How can I make it work with filter, like <code>for d in Device.objects.filter(..some arguments..):</code> ?</p>

<pre><code>from django.utils import timezone
from datetime import timedelta
Device.objects.filter(last_response__lte=timezone.now()-timedelta(seconds=500))
</code></pre>

python django