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)

  

注意两者的区别

相关推荐