base_name
使用viewsets.ModelViewSet时可以没有base_name,但是使用viewsets.ViewSet就必须要base_name
router = routers.DefaultRouter()
router.register(r'users', UserViewSet,base_name='user')
两个报错
法一
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
法二
class UserViewSet(viewsets.ViewSet):
def list(self,request):
queryset = User.objects.all()
serializers = UserSerializer(queryset,many=True)
return Response(serializers.data)
AssertionError: Cannot apply DjangoModelPermissions on a view that does not set .queryset
or have a .get_queryset()
method.
法三
class UserViewSet(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# def list(self, request):
# queryset = User.objects.all()
# serializer = UserSerializer(queryset, many=True)
# return Response(serializer.data)
法四
# ViewSets define the view behavior.
class UserViewSet(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when inst
antiating the serializer.
法五
class UserViewSet(viewsets.ViewSet):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
def list(self, request):
return Response(self.serializer.data)
AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when inst
antiating the serializer.
法六
class UserViewSet(viewsets.ViewSet):
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
AssertionError: Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method.
解决:DjangoModelPermissions
去除以下配置
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
默认permission阻止了没有get_queryset()或queryset属性的viewset被访问
报错变为HyperlinkedIdentityField
根据stackoverflow的回答修改
法七
def list(self, request):
# queryset = User.objects.all()
# serializer = UserSerializer(queryset, many=True)
serializer = UserSerializer(User, context={'request':request})
return Response(serializer.data)
或则
def list(self, request):
queryset = User.objects.all()
# serializer = UserSerializer(queryset, many=True)
serializer = UserSerializer(queryset, context={'request':request}, many=True)
return Response(serializer.data)```
Could not resolve URL for hyperlinked relationship using view name "user-detail". You may
have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
stackoverflow上解决办法
因为它是HyperlinkedModelSerializer,没有定义好相关的view就没有办法通过HyperlinkedModelSerializer获得资源,所以报错了。你应该指明UserSerializer而不是修改URL。
然而我继承的就是系统自带的User
from django.contrib.auth.models import User
点击查看更多内容
5人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦