zoukankan      html  css  js  c++  java
  • xadmin choice filter 多选

    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)
    
  • 相关阅读:
    第16周作业
    第15周作业
    迟到的第14周作业
    第13次作业集
    软件工程结课总结
    第13次作业
    第12次作业
    第11次作业--字符串的处理
    第10次作业(2)
    第十次作业
  • 原文地址:https://www.cnblogs.com/twfb/p/14892208.html
Copyright © 2011-2022 走看看