<tfoot id='fyTbF'></tfoot>

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

      <bdo id='fyTbF'></bdo><ul id='fyTbF'></ul>
  • <small id='fyTbF'></small><noframes id='fyTbF'>

      1. django admin 为计算字段启用排序

        django admin enable sorting for calculated fields(django admin 为计算字段启用排序)
          • <bdo id='H74Fn'></bdo><ul id='H74Fn'></ul>

              <tbody id='H74Fn'></tbody>
            <tfoot id='H74Fn'></tfoot><legend id='H74Fn'><style id='H74Fn'><dir id='H74Fn'><q id='H74Fn'></q></dir></style></legend>

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

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

                • 本文介绍了django admin 为计算字段启用排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have the following two fields in my db table and model (Model Name: Order):

                  id, branch_id, product_id, cost, quantity, status, ordered_at
                  

                  And I have the following code in my OrderModelAdmin:

                  list_display = (
                      'order_number',
                      'branch',
                      'product',
                      'cost',
                      'quantity',
                      'calculated_total',
                      'status',
                      'ordered_at',
                  )
                  
                  def calculated_total(self, obj):
                      return obj.cost * obj.quantity
                  calculated_total.short_description = _('Total')
                  

                  Now, I want to enable sorting for this field. In reality, all I need to do is to add a column in my SELECT statement:

                   SELECT (t.cost * t.quantity) as TOTAL
                   ORDER BY TOTAL
                  

                  Is there a way I can append an SQL statement for sorting in Django Admin?

                  解决方案

                  It isn't possible to order by the result of the calculated_total method.

                  However, you can set the default ordering for your model admin by overriding the get_queryset method for your model admin, and ordering by an expression that calculates the same thing.

                  class OrderModelAdmin(admin.ModelAdmin):
                      ...
                      def get_queryset(self, request):
                          qs = super(OrderModelAdmin, self).get_queryset(request)
                          qs = qs.order_by(F('cost')*F('quantity'))
                          return qs
                  

                  A similar approach is to annotate the queryset with the total, and then order by that field. Assuming that cost is a DecimalField and quantity is an IntegerField, you need to use ExpressionWrapper to set the output field. See the docs on Using F() with annotations for more info.

                  I don't think it's possible to use total directly in list_display. However, you can alter your calculated_total method to access the annotated field. We set calculated_total.admin_order_field = 'total' so that the Django admin allows you to sort on that column by clicking on it.

                  from django.db.models import F, ExpressionWrapper, DecimalField
                  
                  class OrderModelAdmin(admin.ModelAdmin):
                      list_display = ['name', 'number', 'price', 'calculated_total']
                  
                      def calculated_total(self, obj):
                          return obj.total
                      calculated_total.admin_order_field = 'total'
                  
                      def get_queryset(self, request):
                          qs = super(OrderModelAdmin, self).get_queryset(request)
                          qs = qs.annotate(total=ExpressionWrapper(F('cost')*F('quantity'), output_field=DecimalField())).order_by('total')
                          return qs
                  

                  这篇关于django admin 为计算字段启用排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                • <legend id='7tQMY'><style id='7tQMY'><dir id='7tQMY'><q id='7tQMY'></q></dir></style></legend>

                    <small id='7tQMY'></small><noframes id='7tQMY'>

                      <tfoot id='7tQMY'></tfoot>

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