Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two fields in django queryset

Tags:

mysql

django

How would I do the following SQL query in the Django ORM?

SELECT * FROM my_table WHERE date_added > date_created;
like image 485
David542 Avatar asked Dec 25 '22 04:12

David542


1 Answers

I'm guessing you don't have the date_created available as a variable (as @tttthomasssss assumes) so it will be something like this:

from django.db import models

YourTable.objects.filter(date_added__gt=models.F('date_created')

Docs on F expressions: https://docs.djangoproject.com/en/dev/ref/models/expressions/#f-expressions

like image 135
Wolph Avatar answered Jan 09 '23 07:01

Wolph