Django rest framework 的实践笔记(二)

释放双眼,带上耳机,听听看~!

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

Django rest framework 的实践笔记(二)
法二

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)

Django rest framework 的实践笔记(二)
法四

# 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.

Django rest framework 的实践笔记(二)
解决: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.

Django rest framework 的实践笔记(二)
stackoverflow上解决办法
因为它是HyperlinkedModelSerializer,没有定义好相关的view就没有办法通过HyperlinkedModelSerializer获得资源,所以报错了。你应该指明UserSerializer而不是修改URL。
然而我继承的就是系统自带的User

from django.contrib.auth.models import User

【转自慕课】https://www.imooc.com

Python

Ubuntu apt-get和pip源更换

2022-3-3 1:45:14

Python

Python参数传递,既不是传值也不是传引用

2022-3-3 1:52:00

搜索