Skip to content
Advertisement

Django Rest Framework: Access to passed arguments from views in serializers

Before asking this question, I have seen the following links but they don’t help me at all:

I have an author model that has foreign key to default django user model:

apps/author/models.py

JavaScript

Post model has a foreign key to Author.

apps/posts/models.py

JavaScript

Problem Definition: In order to create a new post, I am getting the current user from self.request.user in views, and pass it to the PostSerializer. But whenever I want to create a new post using the following request to localhost:8000/posts/ I have got an error:

JavaScript

error

error

This is what I have done in apps/posts/views.py:

JavaScript

print(context["author"]) works well and prints out the current author. The problem is that I can’t get it in serializers.

JavaScript

PS: The comments are the ways I have tried but the error is still occurred. How can I fix the problem?

Advertisement

Answer

UPDATE:

I was wrong, there is a simpler solution: https://stackoverflow.com/a/38167148/7285863


That should be simplest way.

JavaScript

It’s default method except passing author data.

You may need to check user has author relation.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement