-1

我在 Django 中有一个 UpdateView,我需要将其限制为仅作者。我无法从请求中获取作者。

class MyPermissionMixin(LoginRequiredMixin, UserPassesTestMixin):
    def dispatch(self, request, *args, **kwargs):
        user_test_result = self.get_test_func()()
        if request.user != ????.user:                 #How do I grab the user('Author')??
            return self.handle_no_permission()
        return super().dispatch(request, *args, **kwargs)
4

1 回答 1

1

通过获取 Autor 实例用户self.get_object()

class MyPermissionMixin(object):

    def dispatch(self, request, *args, **kwargs):
            if request.user != self.get_object().author:         
                return HttpResponseForbidden()

             return super().dispatch(request, *args, **kwargs)
于 2020-04-17T12:16:51.043 回答