在listsizers.py中的views.py和.create()中.post(),. create()和perform_create()之间有什么区别?

时间:2016-11-11 02:56:17

标签: python django django-rest-framework

我对.post()中的.create()perform_create()views.py以及.create()serializers.py的{​​{1}}感到困惑 似乎.create()中的serializers.py获得了validated_data,因此无法检查serializer.is_valid

以下是serializers.py

class PostListSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = [
            'title',
            'content',
            'publish',
        ]

    def create(self, validated_data):
        print("-------------------")
        print("5555555 - create", validated_data)
        return Post.objects.create(**validated_data)

如果我的代码是:

views.py

class TestPostListAPIView(ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

    def post(self, request, *args, **kwargs):
        serializer = PostListSerializer(data=request.data)
        if not serializer.is_valid(raise_exception=False):
            # deal with serializer.errors
            return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("[post]- serializer.validated_data ", serializer.validated_data)
            serializer.save()
        return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)

此过程将.post()中的views.py - > .create()中的serializers.py - > Response到客户

如果我的views.py使用:

class TestPostListAPIView(ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

    # def post(self, request, *args, **kwargs):
    #     serializer = PostListSerializer(data=request.data)
    #     if not serializer.is_valid(raise_exception=False):
    #         # deal with serializer.errors
    #         return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
    #     else:
    #         print("[post]- serializer.validated_data ", serializer.validated_data)
    #         serializer.save()
    #     return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        print("44444-1 perform_create ", serializer)
        if not serializer.is_valid(raise_exception=False):
            print("44444-3 serializer.is_valid() ", serializer.is_valid())
            return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("44444-7 serializer.validated_data ", serializer.validated_data)
        self.perform_create(serializer)
        return Response({ 'Create success'}, status=status.HTTP_201_CREATED)

    def perform_create(self, serializer):
        print("1111-1 perform_create ", serializer)
        if not serializer.is_valid(raise_exception=False):
            print("1111-3 serializer.is_valid() ", serializer.is_valid())
            return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            print("1111-7 serializer.validated_data ", serializer.validated_data)
        user = User.objects.filter(id=8)
        serializer.save(user=user.first())
        print("1111-8 perform_create ")

此过程为.create()中的views.py - > .perform_create()中的views.py - > .create()中的serializers.py - > Response到客户

两者都能奏效! 我发现如果发布无效数据,即使我对.perform_create()部分发表评论,此过程也不会转到.create()部分,因此似乎.perform_create()无法检查 serializer.is_valid()

那么.post()中的.create()views.py之间有什么区别,我应该使用.post()还是应该使用.create().perform_create()
我想知道,因为我必须使用像{'custome error!!!!!!!!!'}这样的客户响应消息, 但我对它们的效果感到困惑,我应该把逻辑放在哪里

1 个答案:

答案 0 :(得分:0)

摘自有关perform_create钩子和其他文档的文档:

  

这些钩子对于设置以下属性特别有用:   隐含在请求中,但不是请求数据的一部分。对于   实例,您可以根据   请求用户,或基于URL关键字参数。