[Django]-How to create a Django queryset filter comparing two date fields in the same model

204👍

F objects.

from django.db.models import F
stale_activities = Activity.objects.filter(updated__gte=F('added_toSolr_date')) 

4👍

The solution of Yuji Tomita, unfortunately, didn’t work.

Consider a model below:

class list(models.Model):
    text = models.CharField(max_length=140)
    created = models.DateTimeField()
    modified = models.DateTimeField()

Queryset:

my_list = todolist.objects.order_by('created').filter(created__gte=F('modified'))

Template:

{% if my_list %}
{% for list in my_list %}
{{ list.text}}
{% endfor %}
{% else %}
<p>there is no list</p>
{% endif %}

In the end I get an empty list, but I know, it is not correct. I have also used the following inside the template (without queryset filter):

{% if list.created|date:'d m y h:i:s' == list.modified|date:'d m y h:i:s' %}

It worked, but I would prefer to see a more elegant solution.

Leave a comment