【问题标题】:Django Admin: Setting list_display conditionallyDjango Admin:有条件地设置 list_display
【发布时间】:2011-03-21 12:24:04
【问题描述】:

有没有像 get_list_display() 这样的管理模型方法,或者我可以有一些条件来设置不同的 list_display 值?

class FooAdmin (model.ModelAdmin):
    # ...
    def get_list_display ():
        if some_cond:
            return ('field', 'tuple',)
        return ('other', 'field', 'tuple',)

【问题讨论】:

    标签: django django-models django-admin


    【解决方案1】:

    ModelAdmin 类有一个名为get_list_display 的方法,它以请求为参数,默认返回该类的list_display 属性。

    所以你可以这样做:

    class ShowEFilter(SimpleListFilter):
        """ A dummy filter which just adds a filter option to show the E column,
            but doesn't modify the queryset.
        """
        title = _("Show E column")
        parameter_name = "show_e"
    
        def lookups(self, request, model_admin):
            return [
                ("yes", "Yes"),
            ]
    
        def queryset(self, request, queryset):
            return queryset
    
    class SomeModelAdmin(admin.ModelAdmin):
    
        list_display = (
          "a",
          "b",
          "c",
          "d",
          "e"
        )
        list_filter = (
            ShowEFilter,
        )
    
    
        def get_list_display(self, request):
            """ Removes the E column unless "Yes" has been selected in the 
                dummy filter.
            """
            list_display = list(self.list_display)
            if request.GET.get("show_e", "no") != "yes":
                list_display.remove("e")
    
            return list_display
    

    【讨论】:

      【解决方案2】:

      您是否尝试过将其设为属性?

      class FooAdmin(admin.ModelAdmin):
          @property
          def list_display(self):
              if some_cond:
                  return ('field','tuple')
              return ('other','field','tuple')
      

      我没有,但它可能有效。

      我也相当肯定你可以拼写出来:

      class FooAdmin(admin.ModelAdmin):
      
          if CONDITION:
              list_display = ('field','tuple')
          else:
              list_display = ('other','field','tuple')
      

      但这个只会在解释 FooAdmin 类时运行检查:但如果您基于 settings.SOME_VALUE 进行测试,例如,它可能会起作用。

      还要注意,第一个示例中的 self 是 FooAdmin 类的实例,而不是 Foo 本身。

      【讨论】:

        【解决方案3】:

        您要覆盖 admin.ModelAdmin 类的 changelist_view 方法:

        def changelist_view(self, request, extra_context=None):
          # just in case you are having problems with carry over from previous
          # iterations of the view, always SET the self.list_display instead of adding
          # to it
        
          if something:
            self.list_display = ['action_checkbox'] + ['dynamic_field_1']
          else:
            self.list_display = ['action_checkbox'] + ['dynamic_field_2']
        
          return super(MyModelAdminClass, self).changelist_view(request, extra_context)
        

        “action_checkbox”是 django 用来在左侧显示操作下拉复选框的信息,因此请确保将其包含在设置 self.list_display 中。像往常一样,如果您只是为 ModelAdmin 类设置 list_display,通常不需要包含它。

        【讨论】:

          猜你喜欢
          • 2017-03-10
          • 2019-07-28
          • 2020-06-07
          • 2017-08-25
          • 1970-01-01
          • 2019-05-13
          • 2012-01-08
          • 1970-01-01
          • 2021-12-24
          相关资源
          最近更新 更多