gpt4 book ai didi

python - 值错误: Cannot assign object user must be a "User" instance

转载 作者:行者123 更新时间:2023-12-01 08:51:54 25 4
gpt4 key购买 nike

这对我来说很奇怪。我在 Django 信号接收器 create_customer_settings 内部设置一个断点来调试它。即使新的 CustomerSettings 对象已成功创建,应用也会引发 ValueError。当我尝试第二次创建它时,出现 UNIQUE 约束错误,但那是因为它已经成功添加。

为什么当isinstance(this_user, User) == True时出现ValueError

这是一个我可以安全捕获并忽略的错误吗?是否因为 CustomerSettings id 与 User id 不同而出现问题?

创建客户设置

@receiver(post_save, sender=User)
def create_customer_settings(sender, instance, created, **kwargs):
if created:
this_user = User.objects.get(id=instance.id)
pdb.set_trace()
CustomerSettings.objects.create(
user=this_user,
email=this_user.email,
date_of_birth="1998-01-04",
in_private_mode=False
)

pdb.set_trace()输出1:

System check identified no issues (0 silenced).
October 29, 2018 - 23:42:57
Django version 2.1.1, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:7000/
Quit the server with CONTROL-C.
> /src/myapp/myapp/models.py(36)create_customer_settings()
-> pdb.set_trace()
(Pdb) this_user
<User: new_user_8080>
(Pdb) isinstance(this_user, User)
True
(Pdb) CustomerSettings.objects.create(
user=this_user,
email=this_user.email,
date_of_birth="1998-01-04",
in_private_mode=False
)
*** ValueError: Cannot assign
"<CustomerSettings: CustomerSettings object (6)>":
"CustomerSettings.user" must be a "User" instance.

pdb.set_trace()输出2:

(Pdb) this_user.is_anonymous
False
(Pdb) this_user
<User: new_user_8080>
(Pdb) CustomerSettings.objects.create(
user=this_user,
email=this_user.email,
date_of_birth="1998-01-04",
in_private_mode=False
)
*** django.db.utils.IntegrityError:
UNIQUE constraint failed: myapp_customersettings.user_id
(Pdb) CustomerSettings.objects.all()
<QuerySet [<CustomerSettings: CustomerSettings object (1)>,
<CustomerSettings: CustomerSettings object (2)>,
<CustomerSettings: CustomerSettings object (3)>,
<CustomerSettings: CustomerSettings object (4)>,
<CustomerSettings: CustomerSettings object (5)>,
<CustomerSettings: CustomerSettings object (6)>]>
(Pdb) CustomerSettings.objects.all().values()
<QuerySet [
{'id': 1, 'user_id': 1, 'timezone': 'America/New_York', 'email':...
...
...
...
, {'id': 6, 'user_id': 15, 'timezone': '', 'email': 'new_user_8080@gmail.com',
'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}]>
(Pdb) CustomerSettings.objects.filter(user_id=15)
<QuerySet [<CustomerSettings: CustomerSettings object (6)>]>
(Pdb) CustomerSettings.objects.filter(user_id=15).values()
<QuerySet [{'id': 6, 'user_id': 15, 'timezone': '', 'email': 'new_user_8080@gmail.com', 'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}]>
(Pdb) CustomerSettings.objects.filter(user_id=15).values()[0]
{'id': 6, 'user_id': 15, 'timezone': '', 'email': 'new_user_8080@gmail.com', 'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}
(Pdb) CustomerSettings.objects.filter(user_id=15).values()[0]['id']
6
(Pdb) User.objects.get(id=15)
<User: new_user_8080>
(Pdb)

CustomerSettings 型号:

class CustomerSettings(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True, null=False, blank=False)
timezone = models.CharField(max_length=50, choices=TIMEZONE_CHOICES, blank=False, null=False)
email = models.EmailField(
verbose_name='email address',
max_length=255, blank=False, null=False,
unique=True)
email_confirmed = models.BooleanField(default=False)
date_of_birth = models.DateField()
first_name = models.CharField(max_length=50, null=False, blank=False)
last_name = models.CharField(max_length=50, null=False, blank=False)
in_private_mode = models.BooleanField()

最佳答案

仔细检查您发布的代码是否是您正在运行的代码。错误消息ValueError: Cannot assign "<CustomerSettings: CustomerSettings object (6)>": "CustomerSettings.user" must be a "User" instance意味着您正在尝试创建 CustomerSettings实例,其中 user属性的实例为 CustomerSettings那是 id 是 6。

我的猜测是您正在运行的内容和您发布的内容之间存在脱节。如果没有,则有东西正在调用 create_customer_settings其中instance参数是 CustomerSettings 的实例.

关于python - 值错误: Cannot assign object user must be a "User" instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com