Esempio n. 1
0
 def update(self, request, *args, **kwargs):
     # get_object 是 DRF 包装的一个函数,会在找不到的时候 raise 404 error
     # 所以这里无需做额外判断
     comment = self.get_object()
     serializer = CommentSerializerForUpdate(
         instance=comment,
         data=request.data,
     )
     if not serializer.is_valid():
         return Response(
             {
                 'success': False,
                 'message': 'Please check input.',
                 'errors': serializer.errors,
             },
             status=status.HTTP_400_BAD_REQUEST)
     # save 方法会触发 serializer 里的 update 方法,点进 save 的具体实现里可以看到
     # save 是根据 instance 参数有没有传来决定是触发 create 还是 update
     comment = serializer.save()
     return Response(
         CommentSerializer(comment, context={
             'request': request
         }).data,
         status=status.HTTP_200_OK,
     )
Esempio n. 2
0
    def update(self, request, *args, **kwargs):
        # feed the comment instance to the serializer to update
        # if not feed instance, serializer create object
        comment = self.get_object()
        serializer = CommentSerializerForUpdate(
            data=request.data,
            instance=comment,
        )
        # validate the input of the update
        if not serializer.is_valid():
            return Response(
                {
                    'success': False,
                    'message': 'Invalid Comment Update Input',
                    'error': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        # save the update
        updated_comment = serializer.save()
        serializer = CommentSerializer(
            instance=updated_comment,
            context={'request': request},
        )
        return Response(serializer.data, status=status.HTTP_200_OK)
 def update(self, request, *args, **kwargs):
     serializer = CommentSerializerForUpdate(
         instance=self.get_object(),
         data=request.data,
     )
     if not serializer.is_valid():
         return Response({
             'message': 'Please check input'
         }, status=status.HTTP_400_BAD_REQUEST)
     comment = serializer.save()
     return Response(
         CommentSerializer(comment, context={'request': request}).data,
         status=status.HTTP_200_OK,
     )
Esempio n. 4
0
    def update(self, request, *args, **kwargs):

        serializer = CommentSerializerForUpdate(
            instance=self.get_object(),
            data=request.data
        )
        if not serializer.is_valid():
            return Response({
                "success": False,
                "message": "Please check inputs.",
                "errors": serializer.errors,
            }, status=HTTP_400_BAD_REQUEST)
        comment = serializer.save()

        return Response(CommentSerializer(comment).data, status=HTTP_200_OK)
Esempio n. 5
0
    def update(self, request, *args, **kwargs):
        # get_object will raise 404 error if not found
        serializer = CommentSerializerForUpdate(
            instance=self.get_object(),
            data=request.data,
        )
        if not serializer.is_valid():
            raise Response({'message': 'Please check input'},
                           status=status.HTTP_400_BAD_REQUEST)

        comment = serializer.save()

        return Response(
            CommentSerializer(comment).data,
            status=status.HTTP_200_OK,
        )
Esempio n. 6
0
 def update(self, request, *args, **kwargs):
     # get_object is DRF wrapped method, raise 404 error when cannot find
     serializer = CommentSerializerForUpdate(
         instance=self.get_object(),
         data=request.data,
     )
     if not serializer.is_valid():
         return Response({
             'message': 'Please check input.'
         }, status=status.HTTP_400_BAD_REQUEST)
     # save() will trigger update()
     # save() depends on instance has param or not to decide create() or update()
     comment = serializer.save()
     return Response(
         CommentSerializer(comment, context={'request': request}).data,
         status=status.HTTP_200_OK,
     )
Esempio n. 7
0
 def update(self, request, *args, **kwargs):
     # get_object is a wrapped function in DRF, will raise 404 error when the object cannot be found
     # so no need to do extra check whether the comment object exists
     serializer = CommentSerializerForUpdate(
         instance=self.get_object(),
         data=request.data,
     )
     if not serializer.is_valid():
         return Response({
             'message': 'Please check input',
         }, status=status.HTTP_400_BAD_REQUEST)
     # save will trigger the update method of the serializer
     # whether to trigger create() or update() depends on the existence of the parameter instance
     comment = serializer.save()
     return Response(
         CommentSerializer(comment, context={'request': request}).data,
         status=status.HTTP_200_OK,
     )
Esempio n. 8
0
    def update(self, request, *args, **kwargs):
        serializer = CommentSerializerForUpdate(instance=self.get_object(),
                                                data=request.data)

        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input',
                    'error': serializer.errors,
                },
                status=400)

        comment = serializer.save()
        return Response(
            {
                'success': True,
                'comment': CommentSerializer(comment).data,
            },
            status=200)
Esempio n. 9
0
 def update(self, request, *args, **kwargs):
     # get_object 是DRF包装的一个函数,会在找不到的时候,raise 404 error
     # 所以这里无需做额外的判断
     serializer = CommentSerializerForUpdate(
         instance=self.get_object(),
         data = request.data,
     )
     if not serializer.is_valid():
         return  Response({
             'message': 'Please check input',
         }, status=status.HTTP_400_BAD_REQUEST)
     # save 的方法会触发 serializer 里面的 update 方法,点进save的具体实现里面可以看到
     # save 是根据 instance 参数有没有传来巨鼎是触发 create 还是 update
     comment = serializer.save()
     return Response(
         CommentSerializer(
             comment,
             context = {'request': request},
         ).data,
         status=status.HTTP_200_OK,
     )
Esempio n. 10
0
 def update(self, request, *args, **kwargs):
     # get_object is a function wrapped by DRF. It will use filter id to
     # filter object out of queryset, raise 404 error when not found
     # There is no extra code needed
     comment = self.get_object()
     serializer = CommentSerializerForUpdate(
         instance=comment,
         data=request.data,
     )
     if not serializer.is_valid():
         raise Response({'message': 'Please check input'},
                        status=status.HTTP_400_BAD_REQUEST)
     # save() will trigger update() in serializer, click into save() to see
     # implementation
     # save() will trigger create() or update() based on whether instance
     # param is filled or not
     comment = serializer.save()
     return Response(
         CommentSerializer(comment, context={
             'request': request
         }).data,
         status=status.HTTP_200_OK,
     )