How to achieve this manipulation in the Django-adm

2019-08-09 06:39发布

问题:

When the user clicks on 'Users - Pending' in the picture below:

he should be redirected to the page with the filter by staff status set to 'No' and not 'All' which is the default. The url of that, which appears on clicking 'No' is this http://127.0.0.1:8000/admin/auth/pending/?is_staff__exact=0:

What do I need to change where to achieve this?

回答1:

def get_queryset(self, request):

q = super(UserAdmin, self).get_queryset(request)
if request.user.is_superuser:
    return q
return q.filter(is_staff = False)


回答2:

Add this def in your 'UserPendingAdmin' class:

def get_queryset(self, request):
    """Limit list display to instances that belong to the request's user."""
    qs = super(UserPendingAdmin, self).get_queryset(request)
    # this if you want superuser to see all
    if request.user.is_superuser:
        return qs
    return qs.filter(user__staff_status = False)