【问题标题】:django choicefield filter in admin panel管理面板中的 django 选择字段过滤器
【发布时间】:2016-02-15 20:58:49
【问题描述】:

默认情况下,django 管理员的list_filter 提供模型选择中可用的所有过滤器。但除了那些我想要一个过滤器之外,我们可以说它是“无”过滤器。

class Mymodel:
    char choice field (choices=(('1', 'txt1', '2', 'txt2')), null=True)

class MymodelAdmin(admin.ModelAdmin):
    ...
    list_filter = [..., choice_field, ...]
    ...

这将在管理面板中设置三个过滤器(右侧过滤器)All, 'txt1', 'txt2'。正确的? 如果没有从选项中分配值,我想要一个过滤器“无”。

到目前为止我尝试了什么..

class ChoiceFieldFilter(admin.filters.ChoicesFieldListFilter):

    def __init__(self, *args, **kwargs):
        super(ChoiceFieldFilter, self).__init__(*args, **kwargs)

        self.lookup_val = [('', 'None')]

    def queryset(self, request, queryset):
        print self.lookup_val
        print  self.field.flatchoices
        if self.lookup_val == '':
            return queryset.filter(choice_field='')
        else:
            return queryset.filter(choice_field=self.lookup_val)

    def choices(self, cl):
        pass

然后在管理类中

list_filter = [..., ('choice_field', ChoiceFieldFilter), ...]

但它不起作用,我无法在 django admin 中看到 None 过滤器

【问题讨论】:

    标签: python django django-admin


    【解决方案1】:

    您不必制作自定义列表过滤器。只需使用 django 的 AllValuesFieldListFilter

    from django.contrib.admin.filters import AllValuesFieldListFilter
    ...
    list_filter = [..., ('choice_field', AllValuesFieldListFilter)]
    ...
    

    【讨论】:

      【解决方案2】:

      默认情况下,admin.AllValuesFieldListFilter 返回一个选项的值,而不是选项的详细名称。因此,为了解决它,请使用修改后的 admin.AllValuesFieldListFilter。

      class AllValuesChoicesFieldListFilter(admin.AllValuesFieldListFilter):
      
          def choices(self, changelist):
              yield {
                  'selected': self.lookup_val is None and self.lookup_val_isnull is None,
                  'query_string': changelist.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
                  'display': _('All'),
              }
              include_none = False
      
              # all choices for this field
              choices = dict(self.field.choices)
      
              for val in self.lookup_choices:
                  if val is None:
                      include_none = True
                      continue
                  val = smart_text(val)
                  yield {
                      'selected': self.lookup_val == val,
                      'query_string': changelist.get_query_string({
                          self.lookup_kwarg: val,
                      }, [self.lookup_kwarg_isnull]),
      
                      # instead code, display title
                      'display': choices[val],
                  }
              if include_none:
                  yield {
                      'selected': bool(self.lookup_val_isnull),
                      'query_string': changelist.get_query_string({
                          self.lookup_kwarg_isnull: 'True',
                      }, [self.lookup_kwarg]),
                      'display': self.empty_value_display,
                  }
      

      用法:

      list_filter = (
              ('country_origin', AllValuesChoicesFieldListFilter),
          )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-20
        • 2015-03-12
        • 2018-02-25
        • 2010-12-07
        • 2013-10-20
        • 2012-09-19
        • 1970-01-01
        • 2019-01-31
        相关资源
        最近更新 更多