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

更多相关文章

  1. 008 Python基本语法元素小结
  2. Django代理模型返回父模型
  3. 转换器4:手写PHP转Python编译器,语法解析部分
  4. Tensorflow:恢复图形和模型,然后在单个图像上运行评估
  5. [caffe(二)]Python加载训练caffe模型并进行测试2
  6. Linux nginx 配置 location 语法 正则表达式
  7. 新人求助:tensorflow官方文档里构建第一个多层卷积网络,训练和评估
  8. linux shell基础语法
  9. 双插槽与单插槽内存模型?

随机推荐

  1. 比较C#和JAVA中面向对象语法的区别
  2. 有关UML的基础介绍
  3. C#中匿名对象与var以及动态类型 dynamic
  4. 比较TCP与UDP之间的区别
  5. c# webservice中访问http和https的wsdl以
  6. C语言在屏幕上显示内容
  7. .NET Core2.0小技巧之MemoryCache问题修
  8. .NET CORE如何动态调用泛型解决方法
  9. C#中委托和匿名委托的具体介绍
  10. asp.net mvc如何动态编译生成Controller