1
You could pass author_id
to serializer save()
method:
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = serializers.ArticlesSerializer(data=data)
if serializer.is_valid():
serializer.save(author=author.id)
One advice, Django Rest Framework allows you write less more code. You donβt need regular Django views with JSONParser
, JsonResponse
etc.
Use APIView
class or api_view
decorator instead or even use viewsets
it can reduce the function lines to 5-6
Source:stackexchange.com