【问题标题】:alueError: Cannot assign value-field-must-be-a-object-instancevalueError:无法分配值字段必须是对象实例
【发布时间】:2021-08-06 09:12:53
【问题描述】:

我收到以下错误

ValueError: Cannot assign "(<UP:testUser>, True)": "C.pI" must be a "UP" instance.

【问题讨论】:

    标签: django unit-testing django-models


    【解决方案1】:

    它返回元组:

    profile, _ = UserProfile.objects.update_or_create(gender = 'F',
                                               defaults={
                                               'user': user})
    

    profile 是您的用户,_ 是布尔值(如果它已创建或更新)。

    【讨论】:

    • 是的,没错。那么但是,为什么它会抛出错误呢?很明显,它创建了 UserProfile,然后给出了一个值错误。
    • @NikhitaSareen 说你尝试分配 (&lt;UserProfile: testUser&gt;, True) 这是一个元组,你需要 &lt;UserProfile: testUser&gt;
    • 只需用我上面写的更新create_user_profile,它就会起作用。
    猜你喜欢
    • 2020-04-04
    • 2018-06-03
    • 1970-01-01
    • 2019-11-14
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多