DRF request.data没有属性_mutable

Anu*_*TBE 3 python django django-rest-framework django-rest-auth django-oauth

我正在使用 Django 2.x 和 Django REST 框架。

我用来django-oauth-toolkit启用OAuth2身份验证、django-rest-auth登录和django-allauth用户注册。

我想在用户成功注册时在响应中生成访问令牌。为此,我使用自定义注册视图。

为此,我创建了一个函数 utils,例如

def generate_token(request, user):
    # Get OAuth application to use
    application_: Application = Application.objects.filter(
        client_type=Application.CLIENT_CONFIDENTIAL,
        authorization_grant_type=Application.GRANT_PASSWORD
    ).first()

    if not application_:
        raise Exception('No OAuth2 Application is setup')

    auth_data = {
        'username': user.username,
        'password': password,
        'grant_type': 'password',
        'client_id': application_.client_id,
        'client_secret': application_.client_secret
    }

    if request.data:

        mutable = request.data._mutable
        request.data._mutable = True
        request.data.update(auth_data)
        request.data._mutable = mutable

    if request.POST:
        mutable = request.POST._mutable
        request.POST._mutable = True
        request.POST.update(auth_data)
        request.POST._mutable = mutable

    return TokenView().create_token_response(request=request)
Run Code Online (Sandbox Code Playgroud)

当端点被邮递员击中时,它工作正常并且request.data具有_mutable属性。

但是当它被 Angular 应用程序击中时,它会给出错误

'dict' object has no attribute '_mutable'
Run Code Online (Sandbox Code Playgroud)

并且错误指向mutable = request.data._mutable

为什么_mutable某些请求缺失?

编辑2:请求标头

Postman发送的请求头为

Content-Type:"application/json"
Accept:"application/json, text/plain, /"
User-Agent:"PostmanRuntime/7.15.2"
Cache-Control:"no-cache"
Postman-Token:"b4461728-a6a9-48da-a4fa-3894920b5484"
Host:"api.staging.scanova.io"
Cookie:"messages="660481c3ce8c48b1884641ffdec0a3f701cdc9cf$..."; csrftoken=tNa6o6RDkEUTBti1cDJCvgV5oLG84qczgADeDfSY7hROsLP6cmhIgQKaamPQU7Xy; sessionid=r0l8kh58n8i14quyemj60ur6i59uhy1i"
Accept-Encoding:"gzip, deflate"
Content-Length:635
Connection:"keep-alive"
Run Code Online (Sandbox Code Playgroud)

来自 Angular 的请求标头是

Accept: application/json, text/plain, */*
Authorization: false false
Content-Type: application/json
Origin: https://localhost:4200
Referer: https://localhost:4200/auth/register
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
Run Code Online (Sandbox Code Playgroud)

编辑3:请求类型

从端点调用的主视图是CreateAPIViewgenerate_token从此视图调用,用于TokenView生成访问令牌。使用TokenViewDjango 的通用View.

dra*_*vic 6

对于后代(尽管这个问题相当古老):

看起来 django/DRF 处理multipart/form-dataapplication/json内容类型的方式不同,足以引起问题。

因此,即使使用相同的端点(视图集)并且根据表单是否作为多部分或 app/json 发送,request.data也将是不同的对象。

在一种情况下,它是一个“正常”的字典,而在另一种情况下,它是一种QueryDict类型。因此,为了使用 hacky_mutable开/关,需要进行与此类似的额外检查:

...
# one can also use:
# if 'multipart/form-data' in request.META.get('CONTENT_TYPE'):
# instead of the condition below
if isinstance(request.data, QueryDict):
  auth_data = json.dumps(auth_data) # <----- QueryDict expects string values

request.POST._mutable = True # <----- mutable needs to be modified on POST and not on data
request.data.update(auth_data)
request.POST._mutable = False
...
Run Code Online (Sandbox Code Playgroud)