AttributeError: 'IsInvited' object has no attribute 'request'
AttributeError: 'IsInvited' object has no attribute 'request'
我有一个像这样的模型视图集:
class MeetingViewSet(viewsets.ModelViewSet):
queryset = Meeting.objects.all()
serializer_class = MeetingSerializer
permission_classes =[IsAuthenticated & IsInvited | IsOwner]
和权限:
class IsInvited(BasePermission):
message = 'you must have been invited to see this meeting'
def has_object_permission(self, object, request, view):
if self.request.method == 'GET' and object.is_invited(self.request.user):
return True
return False
class IsOwner(BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_authenticated
def has_object_permission(self, object, request, view):
if self.request.user == object.host:
return True
return False
但我收到了这个错误:
Internal Server Error: /api/meetings/1/
Traceback (most recent call last):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/mixins.py", line 54, in retrieve
instance = self.get_object()
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/generics.py", line 99, in get_object
self.check_object_permissions(self.request, obj)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 345, in check_object_permissions
if not permission.has_object_permission(request, self, obj):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/permissions.py", line 81, in has_object_permission
self.op1.has_object_permission(request, view, obj) or
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/permissions.py", line 64, in has_object_permission
self.op2.has_object_permission(request, view, obj)
File "/home/admin1/mizbanproject/mizban_events/meeting/api/permissions.py", line 14, in has_object_permission
return request.method == 'GET' and object.is_invited(request.user)
AttributeError: 'MeetingViewSet' object has no attribute 'method'
IsOwner 完美运行,我只在 IsInvited 中遇到这个错误,我很困惑,不知道如何解决这个错误。
has_object_permission
方法不能使用self.request
获取请求,因为BasePermission
不包含request
:请求作为参数传递,所以你可以实现这个 wsith:
def has_object_permission(self<b>, request, view, object</b>):
return <strong>request</strong>.method == 'GET' and object.is_invited(<strong>request</strong>.user)
# ↑ no self.request ↑
参数的顺序也不一样:分别是self
、object
、view
和rquest
。
您的权限可能如下所示:
class IsInvited(BasePermission):
message = 'you must have been invited to see this meeting'
def has_object_permission(self<strong>, request, view, object</strong>):
if return <strong>request</strong>.method == 'GET' and object.is_invited(request.user)
class IsOwner(BasePermission):
def has_permission(self<strong>, request, view</strong>):
return request.user and request.user.is_authenticated
def has_object_permission(self<strong>, request, view, object</strong>):
return <strong>request.</strong>user == object.host
我有一个像这样的模型视图集:
class MeetingViewSet(viewsets.ModelViewSet):
queryset = Meeting.objects.all()
serializer_class = MeetingSerializer
permission_classes =[IsAuthenticated & IsInvited | IsOwner]
和权限:
class IsInvited(BasePermission):
message = 'you must have been invited to see this meeting'
def has_object_permission(self, object, request, view):
if self.request.method == 'GET' and object.is_invited(self.request.user):
return True
return False
class IsOwner(BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_authenticated
def has_object_permission(self, object, request, view):
if self.request.user == object.host:
return True
return False
但我收到了这个错误:
Internal Server Error: /api/meetings/1/
Traceback (most recent call last):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/mixins.py", line 54, in retrieve
instance = self.get_object()
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/generics.py", line 99, in get_object
self.check_object_permissions(self.request, obj)
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/views.py", line 345, in check_object_permissions
if not permission.has_object_permission(request, self, obj):
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/permissions.py", line 81, in has_object_permission
self.op1.has_object_permission(request, view, obj) or
File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/rest_framework/permissions.py", line 64, in has_object_permission
self.op2.has_object_permission(request, view, obj)
File "/home/admin1/mizbanproject/mizban_events/meeting/api/permissions.py", line 14, in has_object_permission
return request.method == 'GET' and object.is_invited(request.user)
AttributeError: 'MeetingViewSet' object has no attribute 'method'
IsOwner 完美运行,我只在 IsInvited 中遇到这个错误,我很困惑,不知道如何解决这个错误。
has_object_permission
方法不能使用self.request
获取请求,因为BasePermission
不包含request
:请求作为参数传递,所以你可以实现这个 wsith:
def has_object_permission(self<b>, request, view, object</b>):
return <strong>request</strong>.method == 'GET' and object.is_invited(<strong>request</strong>.user)
# ↑ no self.request ↑
参数的顺序也不一样:分别是self
、object
、view
和rquest
。
您的权限可能如下所示:
class IsInvited(BasePermission):
message = 'you must have been invited to see this meeting'
def has_object_permission(self<strong>, request, view, object</strong>):
if return <strong>request</strong>.method == 'GET' and object.is_invited(request.user)
class IsOwner(BasePermission):
def has_permission(self<strong>, request, view</strong>):
return request.user and request.user.is_authenticated
def has_object_permission(self<strong>, request, view, object</strong>):
return <strong>request.</strong>user == object.host