[英]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.
我通过覆盖序列化器中的create方法来弄清楚这一点。 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: 序列化器传递给上下文字典,该字典包含视图实例,因此您可以通过执行以下
user
来吸引user
:
request = self.context.get('request', None)
if request is not None:
email = request.user.email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.