Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在序列化程序中需要context={';request';:self.request}?_Python_Django_Django Rest Framework - Fatal编程技术网

Python 为什么在序列化程序中需要context={';request';:self.request}?

Python 为什么在序列化程序中需要context={';request';:self.request}?,python,django,django-rest-framework,Python,Django,Django Rest Framework,今天我深入研究了django rest auth包。我不知道context={'request':self.request}在get\u response和post函数中的作用 他们说上下文参数用于将额外的上下文包含到序列化程序中。但在代码下面,似乎没有必要放置上下文参数。我错过了什么吗 背景: django rest认证: 有时您需要序列化程序方法中的请求数据。对于这种情况,您可以向序列化程序的上下文提供请求。例如,如果您查看,您将在savemethoduse\u https选项中看到,该选项

今天我深入研究了django rest auth包。我不知道
context={'request':self.request}
get\u response
post
函数中的作用

他们说上下文参数用于将额外的上下文包含到序列化程序中。但在代码下面,似乎没有必要放置上下文参数。我错过了什么吗

背景:

django rest认证:


有时您需要序列化程序方法中的请求数据。对于这种情况,您可以向序列化程序的上下文提供请求。例如,如果您查看,您将在
save
method
use\u https
选项中看到,该选项基于通过上下文参数传递的请求进行计算:

def save(self):
    request = self.context.get('request')
    # Set some values to trigger the send_email method.
    opts = {
        'use_https': request.is_secure(),
        'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
        'request': request,
    }
您还可以检查用户是否经过身份验证,这取决于它是否在序列化程序级别返回一个或另一个数据

def save(self):
    request = self.context.get('request')
    # Set some values to trigger the send_email method.
    opts = {
        'use_https': request.is_secure(),
        'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
        'request': request,
    }