假设我有一个看起来像这样的模型:
class StockRequest(models.Model):
amount_requested = models.PositiveIntegerField(null=True)
amount_approved = models.PositiveIntegerField(null=True)
有什么方法可以使 Django 查询显示所有请求,其中在特定对象/行上的 amount_requested 和 amount_approved 之间存在某种关系?
在 SQL 中它会很简单:
select * from stockrequest where amount_requested = amount_approved;
或
select * from stockrequest where amount_requested = amount_approved;
在 Django 中,我不确定它是否可以完成,但我会想象像下面这样的东西(注意:语法完全组成并且不起作用)。
StockRequest.objects.filter(amount_requested="__amount_approved")
最佳答案
from django.db.models import F
StockRequest.objects.filter(amount_requested=F("amount_approved"))
http://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model
关于python - 如何进行查询以过滤其中一列等于同一表中另一列的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886819/