def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) comment = serializer.save() NotificationService.send_comment_notification(comment) return Response(CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # note that here we need to add 'data=' to specify that those params are to be passed to data # because by default the first parameter is meant to be instance serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response({ 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save will trigger the create method inside serializer comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={'request': request}).data, status=status.HTTP_201_CREATED, )