如何在admin.py中按用户选择list_filter?

时间:2018-09-08 22:02:48

标签: django-admin

我希望根据当前用户在admin.py中显示另一个list_filter。我尝试了几种获取request.user的方法,但我不知道。我的admin.py在这里:

class StudentAdmin(admin.ModelAdmin):
    fields = ('nickname', 'student_first',)
    list_display = ('nickname',)

    # I need some function to get the current user
    #if request.user.is_superuser:   didn't work
    if get_user:
        list_filter = ('classroom__teacher',)
    else:
        list_filter = (ClassListFilter,)

    def get_queryset(self, request):
        qs = super(StudentAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            stfilter = qs.all()
        else:
            stfilter = qs.filter(classroom__teacher=request.user)
        return stfilter

1 个答案:

答案 0 :(得分:0)

我认为我的问题是我正在使用get_user,但这是保留的名称/功能。我更改了名称,似乎可以使用

def get_loggedUser(self,request):
    logged_in = request.user
    if logged_in.is_superuser:
        return True
    else:
        return False