Skip to content
Advertisement

How to filter based on referencing values? django

My model

class MyColumn(models.Modle):
   name = Charfield.....
   max = Integerfield....
   min   = Intigerfield....


Class MyCell(models.Model):
   Value = Integerfield.....
   Column = ForeignKey(NyColumn)

My goal is to get values that greater or equal the max or less than or equal the min. But, how to reference the max or min value from inside MyCell.objects.filter

MyCell.objects.filter(value__gte=value__MyColumn__max)

Advertisement

Answer

You can compare your MyCell.Value and MyColumn.max with F() expressions.

from django.db.models import F

MyCell.objects.filter(Value__gte = F('Column__max'))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement