Is there a way to set request_body
for @swagger_auto_schema
to only be part of a Serializer? The reasoning being as you can see below the creator is set by the current user object passed by authenticator to the post_create
view, but if I set request_body
to the PostSerializer
that'll be confusing for others, because they'll assume it needs a creator
attribute, even though that's parsed from the user. Is there a way I can set request_body
for this endpoint that uses @api_view
with some of PostSerializer
?
view.py
@api_view(['POST'])
@swagger_auto_schema(
operation_description="Create a post object"
)
def post_create(request):
try:
request.data['creator'] = str(request.user.uuid)
post_serializer = PostSerializer(data=request.data)
if post_serializer.is_valid(raise_exception=True):
post_obj = post_serializer.save()
except ValidationError as e:
return Response(dict(error=str(e),
user_message=error_message_generic),
status=status.HTTP_400_BAD_REQUEST)
return Response(post_serializer.data, status=status.HTTP_201_CREATED)
serializer.py
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('creator', 'body', 'uuid', 'created', 'updated_at')