<small id='zjmUQ'></small><noframes id='zjmUQ'>

  • <i id='zjmUQ'><tr id='zjmUQ'><dt id='zjmUQ'><q id='zjmUQ'><span id='zjmUQ'><b id='zjmUQ'><form id='zjmUQ'><ins id='zjmUQ'></ins><ul id='zjmUQ'></ul><sub id='zjmUQ'></sub></form><legend id='zjmUQ'></legend><bdo id='zjmUQ'><pre id='zjmUQ'><center id='zjmUQ'></center></pre></bdo></b><th id='zjmUQ'></th></span></q></dt></tr></i><div id='zjmUQ'><tfoot id='zjmUQ'></tfoot><dl id='zjmUQ'><fieldset id='zjmUQ'></fieldset></dl></div>

    1. <tfoot id='zjmUQ'></tfoot><legend id='zjmUQ'><style id='zjmUQ'><dir id='zjmUQ'><q id='zjmUQ'></q></dir></style></legend>

      • <bdo id='zjmUQ'></bdo><ul id='zjmUQ'></ul>

      1. Django聚合:两个字段相乘的总和

        时间:2024-04-21
          <i id='kSHC4'><tr id='kSHC4'><dt id='kSHC4'><q id='kSHC4'><span id='kSHC4'><b id='kSHC4'><form id='kSHC4'><ins id='kSHC4'></ins><ul id='kSHC4'></ul><sub id='kSHC4'></sub></form><legend id='kSHC4'></legend><bdo id='kSHC4'><pre id='kSHC4'><center id='kSHC4'></center></pre></bdo></b><th id='kSHC4'></th></span></q></dt></tr></i><div id='kSHC4'><tfoot id='kSHC4'></tfoot><dl id='kSHC4'><fieldset id='kSHC4'></fieldset></dl></div>
              <tbody id='kSHC4'></tbody>
          • <legend id='kSHC4'><style id='kSHC4'><dir id='kSHC4'><q id='kSHC4'></q></dir></style></legend>
                <tfoot id='kSHC4'></tfoot>

                <small id='kSHC4'></small><noframes id='kSHC4'>

                  <bdo id='kSHC4'></bdo><ul id='kSHC4'></ul>
                • 本文介绍了Django聚合:两个字段相乘的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类似这样的模型:

                  I have a model something like this:

                  class Task(models.Model):
                      progress = models.PositiveIntegerField()
                      estimated_days = models.PositiveIntegerField()
                  

                  现在我想在数据库级别进行计算Sum(progress *estimated_days).使用 Django Aggregation 我可以得到每个字段的总和,但不能得到字段乘法的总和.

                  Now I would like to do a calculation Sum(progress * estimated_days) on the database level. Using Django Aggregation I can have the sum for each field but not the summation of multiplication of fields.

                  推荐答案

                  更新: for Django >= 1.8 请按照@kmmbvnr 提供的答案

                  Update: for Django >= 1.8 please follow the answer provided by @kmmbvnr

                  可以使用 Django ORM:

                  it's possible using Django ORM:

                  这是你应该做的:

                  from django.db.models import Sum
                  
                  total = ( Task.objects
                              .filter(your-filter-here)
                              .aggregate(
                                  total=Sum('progress', field="progress*estimated_days")
                               )['total']
                           )
                  

                  注意:如果两个字段的类型不同,请说 integer &float,要返回的类型应该作为Sum

                  Note: if the two fields are of different types, say integer & float, the type you want to return should be passed as the first parameter of Sum

                  这是一个迟到的答案,但我想它会帮助寻找相同的人.

                  It's a late answer, but I guess it'll help someone looking for the same.

                  这篇关于Django聚合:两个字段相乘的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django:配置不当:SECRET_KEY 设置不能为空 下一篇:django html模板找不到静态css和js文件

                  相关文章

                • <legend id='mgCB9'><style id='mgCB9'><dir id='mgCB9'><q id='mgCB9'></q></dir></style></legend>

                  <small id='mgCB9'></small><noframes id='mgCB9'>

                  <tfoot id='mgCB9'></tfoot>

                    1. <i id='mgCB9'><tr id='mgCB9'><dt id='mgCB9'><q id='mgCB9'><span id='mgCB9'><b id='mgCB9'><form id='mgCB9'><ins id='mgCB9'></ins><ul id='mgCB9'></ul><sub id='mgCB9'></sub></form><legend id='mgCB9'></legend><bdo id='mgCB9'><pre id='mgCB9'><center id='mgCB9'></center></pre></bdo></b><th id='mgCB9'></th></span></q></dt></tr></i><div id='mgCB9'><tfoot id='mgCB9'></tfoot><dl id='mgCB9'><fieldset id='mgCB9'></fieldset></dl></div>
                        <bdo id='mgCB9'></bdo><ul id='mgCB9'></ul>