简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM