【问题标题】:How to filter based on referencing values? django如何根据参考值进行过滤? django
【发布时间】:2021-07-21 05:08:49
【问题描述】:

我的模型

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


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

我的目标是获得大于或等于最大值或小于或等于最小值的值。但是,如何从 MyCell.objects.filter 内部引用最大值或最小值

MyCell.objects.filter(value__gte=value__MyColumn__max)

【问题讨论】:

  • minimal reproducible example 的预期之一是,您的示例不应重现与您所面临的问题完全不同的问题。您的示例中有 许多 个拼写错误...
  • 有人回答我很好,是我的目标

标签: python django django-queryset


【解决方案1】:

您可以将MyCell.ValueMyColumn.maxF() expressions 进行比较。

from django.db.models import F

MyCell.objects.filter(Value__gte = F('Column__max'))

【讨论】:

  • , Value__lte = F('Column__min')) 最少
  • 取决于你。您更清楚哪个值属于哪个列。 @ali al-karaawi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 2021-09-19
相关资源
最近更新 更多