python DJANGO - 管理员自定义列表过滤器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python DJANGO - 管理员自定义列表过滤器相关的知识,希望对你有一定的参考价值。

class CategoryListFilter(SimpleListFilter):

    # USAGE
    # In your admin class, pass trhe filter class as tuple for the list_filter attribute:
    #
    # list_filter = (CategoryListFilter,)


    # Human-readable title which will be displayed in the
    # right admin sidebar just above the filter options.
    title = _('categories')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'category'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        list_tuple = []
        for category in Category.objects.get_published_original():
            #print category
            list_tuple.append((category.id, category.translated().title))
        return list_tuple

    def queryset(self, request, queryset):
        """
        Returns the filtered queryset based on the value
        provided in the query string and retrievable via
        `self.value()`.
        """
        # Compare the requested value (either '80s' or 'other')
        # to decide how to filter the queryset.
        if self.value():
            return queryset.filter(category__id=self.value())
        else:
            return queryset

以上是关于python DJANGO - 管理员自定义列表过滤器的主要内容,如果未能解决你的问题,请参考以下文章

管理员中的 Django 自定义列表视图

Django 管理 - 自定义列表外观和 ManyToManyField 键的选择

按 django admin 中的自定义列表显示字段进行列表过滤

在 django 中测试自定义管理操作

如何更改 Django Admin 自定义列表字段标签

Django 1.6 管理面板自定义