【问题标题】:Object 'user' has no atribute 'create' - Django对象“用户”没有属性“创建” - Django
【发布时间】:2018-02-15 14:36:40
【问题描述】:

我有一个关于我遇到的错误的问题。我有一个要根据 Synapse Api 为我的项目发送的查询。我目前正在尝试发送请求以使用 API 创建一个新用户。每次我尝试发送请求时,都会收到一条消息,指出用户对象没有创建。这是错误。

AttributeError at /setup_profile/
type object 'User' has no attribute 'create'
Request Method: POST
Request URL:    http://127.0.0.1:8000/setup_profile/
Django Version: 1.8.6
Exception Type: AttributeError
Exception Value:    
type object 'User' has no attribute 'create'
Exception Location: C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in createUserSynapse, line 1104

这是我拥有的当前代码,它将创建创建新用户的请求。

def createUserSynapse(request):
    args = {
        'email': 'hello@synapsepay.com',
        'phone_number': '555-555-5555',
        'legal_name': 'Hello McHello',
        'note': ':)',  # optional
        'supp_id': '123abc',  # optional
        'is_business': True,
        'cip_tag': 1
    }

    user = User.create(client, **args)
    print(user)

现在我知道,使用普通查询集,我的对象格式如下

User.objects.create(client, **args)

但是当我这样做时,我得到一个错误,上面写着

两个参数被传递,1 是必需的,所以我认为有很多变量被传递......我不确定错误来自哪里......

这是我在使用 User.objects.create(client, ** args) 时遇到的错误

TypeError at /setup_profile/
create() takes 1 positional argument but 2 were given
Request Method: POST
Request URL:    http://127.0.0.1:8000/setup_profile/
Django Version: 1.8.6
Exception Type: TypeError
Exception Value:    
create() takes 1 positional argument but 2 were given
Exception Location: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py in manager_method, line 127

更新

客户端需要传入api调用,它包含以下内容:

import os
from synapse_pay_rest import Client

args = {
    'client_id': 'client_id_...6YiBl',
    'client_secret': 'client_secret_...kC3IF',
    'fingerprint': '...dd48b',
    'ip_address': '127.0.0.1',
    'development_mode':True,
    'logging':False
}

client = Client(**args)

另外,这里是 api 开发人员创建的 api 示例的 github 链接。

https://github.com/synapsepay/SynapsePayRest-Python

客户端必须通过 api 调用传递

【问题讨论】:

    标签: python django synapse


    【解决方案1】:

    Create 方法只有关键字参数。将您的代码重写为:

    User.objects.create(client=client, **args)
    

    更新

    我刚刚发现您正在使用第三方软件包。因此,您需要像 from synapse_pay_rest import User as SynapseUser 这样导入 User 类并在您的代码中使用 SynapseUser:SynapseUser.create(clients, **argss)

    【讨论】:

    • 我试过了,还是不行。我收到以下消息...TypeError at /setup_profile/ 'client' is an invalid keyword argument for this function Request Method: POST Request URL: http://127.0.0.1:8000/setup_profile/ Django Version: 1.8.6 Exception Type: TypeError Exception Value: 'client' is an invalid keyword argument for this function。请查看我的更新并链接到我正在使用的 api。@neverwalkaloner
    • @OmarJandali 您不需要通过客户端来创建用户。就是这样:User.objects.create(**args)
    • 问题是我没有创建本地用户,我正在尝试将 args 传递给 api,为此我需要通过客户端发送带有客户端标头参数的请求... . 我只是不知道该怎么做... @neverwalkaloner
    • @OmarJandali 你确定你导入了正确的用户类:from synapse_pay_rest import User 而不是内置的 django 用户吗?
    • 非常感谢您的帮助。再次。我已将其作为有效的解决方案进行了检查@neverwalkaloner
    猜你喜欢
    • 2020-03-10
    • 2011-12-14
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2019-06-23
    • 2016-02-02
    • 2017-09-13
    相关资源
    最近更新 更多