简体   繁体   English

在Django创建用户时创建model object,UNIQUE约束失败

[英]Create a model object when creating a user in Django, UNIQUE constraint failed

I have this model:我有这个 model:

class Family(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=255, unique=False)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']

And I am also using this function to create a family object when a user is created:当创建用户时,我还使用这个 function 创建一个家庭 object:

@receiver(models.signals.post_save, sender=User)
def user_created(sender, instance, created, **kwargs):
    if created:
        Family.objects.create(user=instance)

I have a couple questions:我有几个问题:

  1. I am getting an django.db.utils.IntegrityError: UNIQUE constraint failed: family_tree_family.name error, how to resolve it?我收到django.db.utils.IntegrityError: UNIQUE constraint failed: family_tree_family.name错误,如何解决?
  2. How to use the **kwargs in user_created function. I create users using curl .如何在user_created function 中使用**kwargs 。我使用curl创建用户。

First, thanks to everyone who helped out.首先,感谢所有提供帮助的人。

I managed to figure the problem, and it was that the field name in family model must also be unique.我设法弄清楚了问题,那就是family model 中的字段name也必须是唯一的。 Even if unique=False attribute was used.即使使用了unique=False属性。 Therefore, the answer is to automatically create a unique name that does not exist before.因此,答案是自动创建一个以前不存在的唯一name Hence, I used instance.username since this value must always be unique:因此,我使用了instance.username ,因为这个值必须始终是唯一的:

@receiver(models.signals.post_save, sender=User)
def user_created(sender, instance, created, **kwargs):
    if created:
        Family.objects.create(user=instance, name=instance.username)

I would like to have a better alternative other than instance.username .我想有一个比instance.username更好的选择。 Maybe if I could use **kwargs somehow.也许如果我能以某种方式使用**kwargs

In user_created try adding instance.family.save() after Family.objects.create(user=instance)在 user_created 中尝试在 Family.objects.create(user=instance) 之后添加 instance.family.save()

@receiver(models.signals.post_save, sender=User)
def user_created(sender, instance, created, **kwargs):
    if created:
        Family.objects.create(user=instance)
    instance.family.save()

and try changing user field in family class if that does not work.如果不起作用,请尝试更改家庭 class 中的用户字段。

user = models.OneToOneField(User, on_delete=models.CASCADE)

Hopefully that helps.希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django / SQLite用户模型UNIQUE约束失败 - Django/SQLite User model UNIQUE constraint failed 在Django用户模型中迁移时,Django UNIQUE约束因OneToOneField失败 - Django UNIQUE constraint failed with OneToOneField while migrate in Django User Model 在 django 中创建新 object 时出现“唯一约束失败:main_chatroom.admin_id” - 'UNIQUE constraint failed: main_chatroom.admin_id' when creating new object in django 创建/编辑/删除用户时,django 管理面板中的 FOREIGN KEY 约束失败。 (使用自定义用户模型。) - FOREIGN KEY constraint failed in django admin panel when creating/editing/deleting a user. (Using custom user model.) django 中的自定义用户 model。 唯一约束失败:users_user.username - Custom User model in django. UNIQUE constraint failed: users_user.username 扩展用户配置文件以包括配置文件模型后,Django抛出UNIQUE Con​​straint Failed错误 - After extending User profile to include a Profile model, Django throws UNIQUE Constraint Failed error Django UNIQUE 约束失败 - Django UNIQUE constraint failed Django-UNIQUE约束失败 - Django - UNIQUE constraint failed Django 模型:当我保存 model 两次时,为什么会出现“IntegrityError UNIQUE constraint failed”? - Django models: Why do I get “IntegrityError UNIQUE constraint failed” when I save the model twice? 在Django创建object时设置当前登录用户为model中的用户 - Set currently logged in user as the user in the model when creating an object in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM