为了账号安全,请及时绑定邮箱和手机立即绑定

Django rest framework 的实践笔记(二)

标签:
Python
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人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消