django - 如何修复 django rest 框架元类冲突
问题描述
我是一个初学者学习 django rest 框架,我只是遇到了这个错误,我似乎找不到解决方法。这是 permissions.py 示例代码
from rest_framework import permissions
class UpdateOwnProfile(permissions, BaseException):
"""Allow user to edit their own profile"""
def has_object_permission(self, request, view, obj):
"""Check if user is trying to update their own profile"""
if request.method in permissions.SAFE_METHODS:
return True
return obj.id == request.user.id
这也是views.py示例代码的示例
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication
from profiles_api import serializers
from profiles_api import models from profiles_api import permissions
class HelloApiView(APIView): """Test Api view""" serializer_class = serializers.HelloSerializer
def get(self, request, format=None):
"""Returns a list of Api features"""
an_apiview = [
'Uses HTTP methods as function (get, post, patch, put, delete)',
'Is similar to a traditional Django view',
'Gives you the most control over your application logic',
'Is mapped manually to URLs',
]
return Response({'message': 'Hello', 'an_apiview': an_apiview})
def post(self, request):
"""Create a hello message with our name"""
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
name = serializer.validated_data.get('name')
message = f'Hello {name}'
return Response({'message': message})
else:
return Response(
serializer.errors,
status = status.HTTP_400_BAD_REQUEST
)
def put(self, request, pk=None):
"""Handling updates of objects"""
return Response({'method': 'PUT'})
def patch(self, request, pk=None):
"""Handle a partial update of an object"""
return Response({'method': 'PATCH'})
def delete(self, request, pk=None):
"""Delete an object"""
return Response({'method': 'DELETE'})
class HelloViewset(viewsets.ViewSet): """Test API Viewset""" serializer_class = serializers.HelloSerializer
def list(self, request):
"""Return a hello message"""
a_viewset = [
'Uses actions (list, create, retrieve, update, partial update'
'Automatically maps to URLs using router'
'provides more functionality with less code'
]
return Response({'message': 'Hello', 'a_viewset': a_viewset})
def create(self, request):
"""Create a new hello message"""
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
name = serializer.validated_data.get('name')
message = f'Hello {name}!'
return Response({'message': message})
else:
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST
)
def retrieve(self, request, pk=None):
"""Handle getting an object by its ID"""
return Response({'http_method': 'GET'})
def update(self, request, pk=None):
"""Handle updating an object"""
return Response({'http_method': 'PUT'})
def partial_update(self, request, pk=None):
"""Handle updating of an object"""
return Response({'http_method': 'PATCH'})
def destroy(self, request, pk=None):
"""Handle removing an object"""
return Response({'http_method': 'DELETE'})
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,)
在运行开发服务器时出现此错误:
class UpdateOwnProfile(permissions, BaseException):TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类
解决方案
permissions
( rest_framework.permissions
) 是类型module
(其类型是type
FWIW),但类型BaseException
是type
(与所有常规类一样)。所以你有一个预期的元类冲突。
据推测,您打算使用permissions.BasePermission
模块中的类:
class UpdateOwnProfile(permissions.BasePermission, BaseException):
...
...
您也可以直接导入和引用该类:
from rest_framework.permissions import BasePermission
class UpdateOwnProfile(BasePermission, BaseException):
...
...
推荐阅读
- django - Django:“用户”和“用户”的实例之间不支持“<”
- excel - 使用条件格式将逗号更改为句点(excel2016)
- database - 如何在 MongoDB 中存储大量单词
- javascript - Vue.JS 导入自定义模块
- android - Flutter Page View 如何不总是从 initialPage 开始?
- javascript - 如何触发单击垫子菜单区域而不是整个垫子菜单按钮?
- maven - maven 解析器有问题:无法创建任务或类型解析
- javascript - puppeteer - 无限滚动页面中的内容在设置溢出后停止工作
- sql - 具有顺序演算的 LAG 函数
- focus - SDL实现鼠标悬停透明控件在linux Xlib下不生成事件