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

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

      1. <tfoot id='L1eML'></tfoot>

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

      2. 使用 Django admin 的自定义列

        时间:2023-11-08
          <tbody id='3l9MN'></tbody>
        • <tfoot id='3l9MN'></tfoot>
        • <legend id='3l9MN'><style id='3l9MN'><dir id='3l9MN'><q id='3l9MN'></q></dir></style></legend>

          <small id='3l9MN'></small><noframes id='3l9MN'>

              <bdo id='3l9MN'></bdo><ul id='3l9MN'></ul>

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

                  本文介绍了使用 Django admin 的自定义列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个模型 Data,关联到这样的表(模型 Data 仅由 IntegerField 组成):

                  I have a model Data, associated to a table like this (The model Data is made up of only IntegerField):

                  subject | year | quarter | sales |
                  ----------------------------------
                     1    | 2010 |   1     | 20    |
                     1    | 2010 |   2     | 100   |
                     1    | 2010 |   3     | 100   |
                     1    | 2010 |   4     | 20    |
                     1    | 2011 |   1     | 30    |
                     1    | 2011 |   2     | 50    |
                     1    | 2011 |   4     | 40    |
                     2    | 2010 |   1     | 30    |
                     2    | 2010 |   2     | 20    |
                   [..-GO ON this way...]
                  

                  我想要一个 django-admin 表,只读有列(当前年份 = 2011,季度 = 1)

                  I want to have a django-admin table, in read-only having columns (current year = 2011, quarter = 1)

                  subject | sales current year | sales current quarter | sales last year | sales current quarter last year |
                  ----------------------------------------------------------------------------------------------------------
                    1     |  110               |  30                   |  240            |  20
                  [AND SO ON]
                  

                  问题是:使用 django-admin 可以做到吗?出路是什么?

                  推荐答案

                  您可以使用 ModelModelAdmin 上的方法作为 list_display.请参阅:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

                  You can use methods on your Model or your ModelAdmin as items for list_display. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

                  由于这些方法在管理员之外也可能有用,我建议将它们添加到您的 Model 中.

                  Since these are methods that might be useful outside the admin, as well, I'd suggest adding them to your Model.

                  from django.db.models import Sum
                  
                  class Data(models.Model):
                      ...
                  
                      # Method used by `get_current_year_sales` and `get_last_year_sales`
                      # to stay DRY. Not for use directly in admin.
                      def get_year_sales(self, year):
                          qs = self.model._default_manager.filter(year=year)
                          sales_agg = qs.aggregate(Sum('sales'))
                          return sales_agg['sales__sum']
                  
                      # Method used by `get_current_quarter_sales` and `get_last_quarter_sales`
                      # to stay DRY. Not for use directly in admin.
                      def get_quarter_sales(self, year, quarter):
                          qs = self.model._default_manager.filter(year=year, quarter=quarter)
                          sales_agg = qs.aggregate(Sum('sales'))
                          return sales_agg['sales__sum']
                  
                      def get_current_year_sales(self):
                          return self.get_year_sales(datetime.now().year)
                      get_current_year_sales.short_description = 'Sales (Current Year)'
                  
                      def get_last_year_sales(self):
                          return self.get_year_sales(datetime.now().year-1)
                      get_last_year_sales.short_description = 'Sales (Last Year)'
                  
                      def get_current_quarter_sales(self):
                          # Determine current quarter logic here as `current_quarter`
                          # `quarter_year` will likely be same as current year here,
                          # but will need to be calculated for previous quarter
                          return self.get_quarter_sales(quarter_year, current_quarter)
                      get_current_quarter_sales.short_description = 'Sales (Current Quarter)'
                  
                      def get_current_quarter_sales(self):
                          # Logic here to determine last quarter as `last_quarter`
                          # Logic to determine what year last quarter was in as `quarter_year`
                          return self.get_quarter_sales(quarter_year, last_quarter)
                      get_last_quarter_sales.short_description = 'Sales (Last Quarter)'
                  

                  short_description 属性决定管理员将在这些方法的行标题中显示的内容.因此,一旦您完成了所有这些,您只需修改 ModelAdminlist_display 属性,例如:

                  The short_description attribute determines what the admin will show as the row header for these methods. So, once you have all this in place, you need only modify your ModelAdmin's list_display attribute like:

                  class DataAdmin(admin.ModelAdmin):
                      ...
                      list_display = ('subject', 'get_current_year_sales', 'get_last_year_sales', 'get_current_quarter_sales', 'get_last_quarter_sales')
                  

                  这篇关于使用 Django admin 的自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                1. <tfoot id='yPoTG'></tfoot>
                2. <i id='yPoTG'><tr id='yPoTG'><dt id='yPoTG'><q id='yPoTG'><span id='yPoTG'><b id='yPoTG'><form id='yPoTG'><ins id='yPoTG'></ins><ul id='yPoTG'></ul><sub id='yPoTG'></sub></form><legend id='yPoTG'></legend><bdo id='yPoTG'><pre id='yPoTG'><center id='yPoTG'></center></pre></bdo></b><th id='yPoTG'></th></span></q></dt></tr></i><div id='yPoTG'><tfoot id='yPoTG'></tfoot><dl id='yPoTG'><fieldset id='yPoTG'></fieldset></dl></div>
                3. <legend id='yPoTG'><style id='yPoTG'><dir id='yPoTG'><q id='yPoTG'></q></dir></style></legend>
                4. <small id='yPoTG'></small><noframes id='yPoTG'>

                    • <bdo id='yPoTG'></bdo><ul id='yPoTG'></ul>
                          <tbody id='yPoTG'></tbody>