class PostSerializer(serializers.ModelSerializer): post_replies = PostReplySerializer(many=True, read_only=True) user = UserSerializer() class Meta: model = Post fields = '__all__'
def post(request): """ Create post reply """ serializer = PostReplySerializerCreate(data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(PostReplySerializer(serializer.instance).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(request, post_reply_id): """ Update post reply """ post_reply = get_object_or_404(PostReply, pk=post_reply_id) serializer = PostReplySerializerUpdate(post_reply, data=request.data, context={'request': request}, partial=True) if serializer.is_valid(): serializer.save() return Response(PostReplySerializer(serializer.instance).data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class PostSerializerFull(PostSerializer): post_replies = PostReplySerializer(many=True, read_only=True)