Puneet
Puneet

Reputation: 215

How do I access the user object inside the model serializer of django-rest-framework?

I have a task for nested insertion. I have figured out that by overriding the create method in serializer. But I need to have user email for insertion. How do I get the email in serializer efficiently?

Upvotes: 0

Views: 413

Answers (1)

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

Serializers are passed a context dictionary, which contains the view instance, so you could get the user by doing something like this:

request = self.context.get('request', None)
    if request is not None:
        email = request.user.email

Upvotes: 3

Related Questions