Python django.contrib.admin 模块,SimpleListFilter() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用django.contrib.admin.SimpleListFilter()

项目:nat64check    作者:sjm-steffann    | 项目源码 | 文件源码
def score_filter(attribute):
    class ScoreFilter(admin.SimpleListFilter):
        title = attribute.replace('_', ' ')
        parameter_name = attribute

        def lookups(self, request, model_admin):
            return (
                ('N', 'Untested'),
                ('U', 'Unreachable'),
                ('B', 'Poor'),
                ('G', 'Mediocre'),
                ('P', 'Good'),
            )

        def queryset(self, request, queryset):
            if self.value() == 'N':
                condition = {attribute: None}
            elif self.value() == 'U':
                condition = {attribute: 0}
            elif self.value() == 'B':
                condition = {attribute + '__gt': 0, attribute + '__lt': 0.8}
            elif self.value() == 'G':
                condition = {attribute + '__gte': 0.8, attribute + '__lt': 0.95}
            elif self.value() == 'P':
                condition = {attribute + '__gte': 0.95}
            else:
                return queryset

            return queryset.filter(**condition)

    return ScoreFilter