首页 > 其他 > 详细

xadmin choice filter 多选

时间:2021-06-17 12:06:20      阅读:12      评论:0      收藏:0      [点我收藏+]

技术分享图片

  1. 在xadmin/filters.py中添加MultiSelectFieldListChoiceFilter
@manager.register
class MultiSelectFieldListChoiceFilter(MultiSelectFieldListFilter):
    def choices(self):
        self.lookup_in_val = (type(self.lookup_in_val) in (tuple, list)) and self.lookup_in_val or list(
            self.lookup_in_val)
        yield {
            ‘selected‘: len(self.lookup_in_val) == 0,
            ‘query_string‘: self.query_string({}, [self.lookup_in_name]),
            ‘display‘: _(‘All‘),
        }
        # for val in self.lookup_choices:
        for lookup, val in self.field.flatchoices:
            yield {
                ‘selected‘: smart_text(lookup) in self.lookup_in_val,
                ‘query_string‘: self.query_string({self.lookup_in_name: ",".join([lookup] + self.lookup_in_val), }),
                ‘remove_query_string‘: self.query_string(
                    {self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val]), }),
                ‘display‘: val,
            }
  1. 在xadmin.py中
def get_filter_fields(cls):
    return [(i.attname.replace(‘_id‘, ‘‘), MultiSelectFieldListChoiceFilter) if i.choices else i.attname.replace(‘_id‘, ‘‘)
            for i in cls._meta.fields if not i.attname.startswith(‘_‘)]

class TestAdmin:
    list_filter = get_filter_fields(Test)

xadmin.site.register(Test, TestAdmin)

xadmin choice filter 多选

原文:https://www.cnblogs.com/twfb/p/14892208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!