DjangoAdmin自定义过滤器

class UserIDFilter(admin.SimpleListFilter):
    # 自定义用户查询过滤器
    title = _('关联用户')

    parameter_name = 'user_id'

    def lookups(self, request, model_admin):
        params = UserProfile.objects.all().values_list('nickname', 'user_id')
        look_choice = []
        for nickname, user_id in list(params):
            temp = ('{}'.format(user_id), _('{}'.format(nickname)))
            look_choice.append(temp)
        return look_choice

    def queryset(self, request, queryset):
        user = self.value()
        if not user:
            return queryset
        return queryset.filter(user=user)

 

posted @ 2019-02-28 16:28  Leslie-x  阅读(907)  评论(0编辑  收藏  举报