How would I do the following SQL in the django ORM:
SELECT * FROM main_titlemaster WHERE id % 4 = 0
The model name is TitleMaster
.
You can use the .extra()
:
TitleMaster.objects.extra(where=['id mod 4=0'])
According to the docs, the correct way of doing this today is using an F()
expression:
Django supports the use of addition, subtraction, multiplication, division, modulo, and power arithmetic with F() objects
This should do the trick:
TitleMaster.objects.annotate(mod_four=F("id") % 4).filter(mod_four=0)