Matt
Matt

Reputation: 3

Calculate percentages in Django Query

I have two integer fields in seperate related models (I don't think it should matter whether they are in the same model or not, as long as they can be related?), called x and y, how could I filter() so I only get results where x > y * 90%?

Upvotes: 0

Views: 932

Answers (1)

Fábio Diniz
Fábio Diniz

Reputation: 10353

Maybe (if they are not in the same model):

class A(models.Model)
    x = ...
    b = OneToOne... 

class B(models.Model)
    y = ....

A.objects.filter(x__gt=F('b.y')*0.9)

reference

Upvotes: 1

Related Questions