gpt4 book ai didi

python - Django 完整性错误

转载 作者:行者123 更新时间:2023-12-01 05:42:31 24 4
gpt4 key购买 nike

我突然遇到完整性错误:“关键‘用户名’的条目‘qw’重复 从这个模型。第1062章

from django.template.defaultfilters import slugify
from django.contrib.auth.models import User

class Customer(User):
slug=models.SlugField(unique=True)
description=models.TextField(null=True)
phone=models.IntegerField(null=True)
id_verified=models.NullBooleanField()
picture=models.ImageField(upload_to='media/customer', null=True)
isWorker=models.BooleanField()

def save(self,*args,**kwargs):
self.slug=slugify(self.username)
super(Customer,self).save(*args, **kwargs)
def __unicode__(self):
return self.username

这里出了什么问题?

最佳答案

来自文档,关于指定自定义用户模型:

You must then provide some key implementation details:

USERNAME_FIELD

A string describing the name of the field on the User model that is used as the unique identifier. This will usually be a username of some kind, but it can also be an email address, or any other unique identifier. The field must be unique (i.e., have unique=True set in its definition).

因此用户名似乎是唯一的,至少在您设置自定义用户名字段之前是这样。

阅读文档

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model

关于python - Django 完整性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164244/

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