我正在尝试添加权限到我的Django Rest Framework项目,但我得到一个错误:AttributeError: type object 'object' has no attribute 'id'
我的 www.example.com 文件看起来像这样:
from rest_framework import permissions
class UpdateOwnProfile(permissions.BasePermission):
"""Allow users to edit their own profile"""
def has_object_permission(self, request, view, obj):
"""Check user is trying to edit their own profile"""
if request.method in permissions.SAFE_METHODS:
return True
return object.id == request.user.id
我的 www.example.com 的com类看起来像这样:
class UserProfileViewSet(viewsets.ModelViewSet):
"""Handle creating and updating profiles"""
serializer_class = serializers.UserProfileSerializer
queryset = models.UserProfile.objects.all()
authentication_classes = (
TokenAuthentication,
)
permission_classes = (
permissions.UpdateOwnProfile,
)
我的 www.example.com 文件:
class UserProfileSerializer(serializers.ModelSerializer):
"""Serializes a user profile object"""
class Meta:
model = models.UserProfile
fields = ("id", "email", "name", "password")
extra_kwargs = {
"password": {
"write_only": True,
"style": {
"input_type": "password"
},
}
}
def create(self, validated_data):
"""Create and return a new user"""
user = models.UserProfile.objects.create_user(
email=validated_data["email"],
name=validated_data["name"],
password=validated_data["password"],
)
return user
def update(self, instance, validated_data):
"""Handle updating user account"""
if "password" in validated_data:
password = validated_data.pop("password")
instance.set_password(password)
return super().update(instance, validated_data)
错误的追溯:
Traceback (most recent call last):
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/renderers.py", line 733, in render
context = self.get_context(data, accepted_media_type, renderer_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/renderers.py", line 664, in get_context
raw_data_put_form = self.get_raw_data_form(data, view, 'PUT', request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/renderers.py", line 552, in get_raw_data_form
if not self.show_form_for_method(view, method, request, instance):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/renderers.py", line 444, in show_form_for_method
view.check_object_permissions(request, obj)
File "/Users/xxxx/Desktop/django-rest-tutorial/django/venv/lib/python3.11/site-packages/rest_framework/views.py", line 345, in check_object_permissions
if not permission.has_object_permission(request, self, obj):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/xxxx/Desktop/django-rest-tutorial/django/profiles_api/permissions.py", line 13, in has_object_permission
return object.id == request.user.id # If True, the user is authenticated, if False, the user has not permissions on that profile, he/she won't be able to make changes
^^^^^^^^^
AttributeError: type object 'object' has no attribute 'id'
由于我是Django Rest Framework的新手,我将非常感谢任何形式的帮助。
提前感谢:)
1条答案
按热度按时间agyaoht71#
object
本身是一种对象(如int
或str
),所以它没有id
。您想调用obj.id
: