xadmin choice filter 多选

2021/6/17 10:26:12

本文主要是介绍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)


这篇关于xadmin choice filter 多选的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程