[Fixed]-Column comparison in Django queries

9👍

You can’t do this right now without custom SQL. The django devs are working on an F() function that would make it possible: #7210 – F() syntax, design feedback required.

0👍

Since I had to look this up based on the accepted answer, I wanted to quickly mention that the F() expression has indeed been released and is available for being used in queries.

This is what the Django documentation on F() says about it:

An F() object represents the value of a model field, transformed value of a model field, or annotated column. It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory.

Instead, Django uses the F() object to generate an SQL expression that describes the required operation at the database level.

The reference for making queries using F() also gives useful examples.

Leave a comment