I have models A
and B
that are like this:
class B(models.Model):
x_count = models.IntegerField()
y_count = models.IntegerField()
class A(models.Model):
name = models.CharField()
b = models.ForeignKey(B)
I am trying to query out all instances of A
that has b
in which the total of x_count
and y_count
is zero. Something like
SELECT A.name FROM A, B WHERE A.b = B.id AND B.x_count + B.y_count > 0
. How can I do that in Django?
https://docs.djangoproject.com/en/1.8/topics/db/queries/#filters-can-reference-fields-on-the-model may help.
You can use those to "compare the value of a model field with another field on the same model."
So your query might look like this (untested)
from django.db.models import F
A.objects.filter(x_count=-F('y_count'))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments