S
S
Stanislav Pilipeyko2021-12-28 20:33:09
Django
Stanislav Pilipeyko, 2021-12-28 20:33:09

Comparing django fields when getting base rows?

I have a table in which there are two fields of the int type.
When receiving records through a filter, I want to compare the values ​​of these fields, here is an example:
queryset.filter(field1__gt=field2)
Ie, get all records where field1 is greater than filed2. Is it possible to implement this through django?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pilipeyko, 2021-12-28
@miniusercoder

I found a solution in the documentation, they pushed it very far.
You need to use method F
Here is the solution to my problem:

from django.db.models import F
queryset.filter(field1__gt=F("field2"))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question