如何创建一个查询来过滤一列等于另一列同一个表的行?
Say I have a model that looks like:
说我的模型看起来像:
class StockRequest(models.Model):
amount_requested = models.PositiveIntegerField(null=True)
amount_approved = models.PositiveIntegerField(null=True)
Is there any way to make a django query that would show me all requests where there is some relationship between amount_requested and amount_approved on a particular object/row?
有没有办法制作一个django查询,它会向我显示特定对象/行上的amount_requested和amount_approved之间存在某种关系的所有请求?
In SQL it would be as simple as:
在SQL中,它将如下所示:
select * from stockrequest where amount_requested = amount_approved;
or
要么
select * from stockrequest where amount_requested = amount_approved;
In Django, I'm not sure if it can be done, but I would imagine something like the below (NOTE: syntax completely made up and does not work).
在Django中,我不确定它是否可以完成,但我会想象下面的内容(注意:语法完全组成并且不起作用)。
StockRequest.objects.filter(amount_requested="__amount_approved")
3 个解决方案
#1
12
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
http://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model
更多相关文章
- 008 Python基本语法元素小结
- Django代理模型返回父模型
- 转换器4:手写PHP转Python编译器,语法解析部分
- Tensorflow:恢复图形和模型,然后在单个图像上运行评估
- [caffe(二)]Python加载训练caffe模型并进行测试2
- Linux nginx 配置 location 语法 正则表达式
- 新人求助:tensorflow官方文档里构建第一个多层卷积网络,训练和评估
- linux shell基础语法
- 双插槽与单插槽内存模型?