django根据不同用户角色设置数据的访问权限

xadmin中的用法:

class NewCourseAdmin:
    list_display = ['name', 'detail', 'degree', 'learn_times', 'students']
    list_editable = ['degree', 'desc']

    def queryset(self):
        qs = super().queryset()
        if not self.request.user.is_superuser:
            qs = qs.filter(teacher=self.request.user.teacher)
        return qs

xadmin.site.register(Course, NewCourseAdmin)

 

admin中的用法:

class NewCourseAdmin(admin.ModelAdmin):
    list_display = ['name', 'detail', 'degree', 'learn_times', 'students']
    list_editable = ['degree', 'desc']

    def get_queryset(self,request):
        qs = super().get_queryset(request)
        if not self.request.user.is_superuser:
            qs = qs.filter(teacher=self.request.user.teacher)
        return qs
admin.site.register(Course, NewCourseAdmin)

  

注意两者的区别

 

posted @ 2020-05-01 23:34  本人小白  阅读(1400)  评论(0编辑  收藏  举报