I am trying to convert this SQL query (which works flawlessly in mySQL) to Django ORM so that it can be used in a PostgreSQL environment (as the SQL query is invalid for PG)