如何在Django的Model字段之间执行算术运算 [英] How to execute arithmetic operations between Model fields in django

查看:554
本文介绍了如何在Django的Model字段之间执行算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序言:



这是在SO中经常出现的问题:





并且也可以应用此处:





我在SO文档,但由于文档将于2017年8月8日关闭,因此我将遵循

当然,我很乐意看到任何不同的东西






问题:



假定以下模型:

  class MyModel(models.Model):
number_1 =模型。 IntegerField()
number_2 = models.IntegerField()
date_1 = models.DateTimeField()
date_2 = models.DateTimeField()

如何在此模型的字段之间执行算术运算?



例如,如何查找:




  • number_1 number_2 MyModel对象?

  • 如何过滤 date_2 date_1 ?


解决方案

F() 表达式可用于执行算术运算(模型字段中的 + - * 等) ,以定义它们之间的代数查找/连接。


An F()对象代表模型字段或带注释的列的值。这样就可以引用模型字段值并使用它们执行数据库操作,而实际上不必将它们从数据库中拉出到Python内存中。


然后解决问题:




  • 两个字段的乘积:

      result = MyModel.objects.all() .annotate(prod = F('number_1')* F('number_2'))

    现在每个结果中的项目有一个名为 prod的额外列,其中包含 number_1 的乘积每个项目分别为number_2


  • 按日差过滤:

     从日期时间导入时间增量

    结果= MyModel.objects.all()。annotate(
    delta = F(' date_2')-F('date_1')
    ).filter(delta__gte = timedelta(days = 10))

    现在,结果中的项目是来自 MyModel 且其 date_2 date_1 大10天或更多天。这些项目有一个名为 delta 的新列,但有不同。


  • 另一种情况:



    我们甚至可以使用 F()表达式对带注释的列进行算术运算,如下所示:

      result = MyModel.objects.all()
    .annotate(sum_1 = Sum('number_1'))
    .annotate(sum_2 = Sum('number_2'))
    .annotate(sum_diff = F('sum_2')-F('sum_1'))



Prologue:

This is a question arising often in SO:

And can also be applied here:

I have composed an example on SO Documentation but since the Documentation will get shut down on August 8, 2017, I will follow the suggestion of this widely upvoted and discussed meta answer and transform my example to a self-answered post.

Of course, I would be more than happy to see any different approach as well!!


Question:

Assume the following model:

class MyModel(models.Model):
    number_1 = models.IntegerField()
    number_2 = models.IntegerField()
    date_1 = models.DateTimeField()
    date_2 = models.DateTimeField()

How can I execute arithmetic operations between fields of this model?

For example, how can I find:

  • The product of number_1 and number_2 of a MyModel object?
  • How to filter items where date_2 is 10 or more days older than date_1?

解决方案

F() expressions can be used to execute arithmetic operations (+, -, * etc.) among model fields, in order to define an algebraic lookup/connection between them.

An F() object represents the value of a model field or annotated column. It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory.

let's tackle the issues then:

  • The product of two fields:

    result = MyModel.objects.all().annotate(prod=F('number_1') * F('number_2'))
    

    Now every item in result has an extra column named 'prod' which contains the product of number_1 and number_2 of each item respectively.

  • Filter by day difference:

    from datetime import timedelta
    
    result = MyModel.objects.all().annotate(
                 delta=F('date_2') - F('date_1')
             ).filter(delta__gte=timedelta(days=10))
    

    Now the items in result are those from MyModel whose date_2 is 10 or more days older than date_1. These items have a new column named delta with that difference.

  • A different case:

    We can even use F() expressions to make arithmetic operations on annotated columns as follows:

    result = MyModel.objects.all()
                            .annotate(sum_1=Sum('number_1'))
                            .annotate(sum_2=Sum('number_2'))
                            .annotate(sum_diff=F('sum_2') - F('sum_1')) 
    

这篇关于如何在Django的Model字段之间执行算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆