如何修复重复的键值违反了唯一约束并在Django管理面板中自定义“添加用户”部分?

时间:2019-08-01 16:19:08

标签: python django python-3.x django-models django-admin

我已经自定义我的用户模型。我在用户模型中添加了许多其他字段。当我尝试通过单击Django管理面板上的“添加用户”按钮来添加用户时,发生以下错误。 以及如何自定义此“添加用户”部分。我想在这里添加更多字段。 Imgur

IntegrityError at /admin/accounts/user/add/
duplicate key value violates unique constraint "accounts_user_email_key"
DETAIL:  Key (email)=() already exists.

我正在使用django2.2,目前正在django中学习复杂的东西。我已经自定义了用户模型。当我尝试通过在django的管理面板中单击此“添加用户”来添加用户时,会发生此错误。

IntegrityError at /admin/accounts/user/add/
duplicate key value violates unique constraint "accounts_user_email_key"
DETAIL:  Key (email)=() already exists.
Request Method: POST
Request URL:    http://127.0.0.1:8000/admin/accounts/user/add/
Django Version: 2.2
Exception Type: IntegrityError
Exception Value:    
duplicate key value violates unique constraint "accounts_user_email_key"
DETAIL:  Key (email)=() already exists.
Exception Location: C:\Program Files\Anaconda\envs\DjangoEnv\lib\site-packages\django\db\backends\utils.py in _execute, line 84
Python Executable:  C:\Program Files\Anaconda\envs\DjangoEnv\python.exe
Python Version: 3.7.3
Python Path:    
['E:\\Project\\ProductHuntSite',
 'C:\\Program Files\\Anaconda\\envs\\DjangoEnv\\python37.zip',
 'C:\\Program Files\\Anaconda\\envs\\DjangoEnv\\DLLs',
 'C:\\Program Files\\Anaconda\\envs\\DjangoEnv\\lib',
 'C:\\Program Files\\Anaconda\\envs\\DjangoEnv',
 'C:\\Users\\khana\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'C:\\Program Files\\Anaconda\\envs\\DjangoEnv\\lib\\site-packages']
Server time:    Thu, 1 Aug 2019 22:11:22 +0600

这是我的代码。

class UserManager(BaseUserManager):
     def create_user(self, username, email, firstname, lastname, profile_photo,mobile_phone_number, password=None, is_staff=False, is_active=True, is_admin=False, is_moderator=False):
        if not email:
            raise ValueError("User must have an Email")
        if not username:
            raise ValueError("User must have an Username")
        if not password:
            raise ValueError("User must have an Password")
        if not firstname:
            raise ValueError("User must have an Firstname")            
        if not lastname:
            raise ValueError("User must have an Lastname")

        user_obj = self.model(username = username,
                                email = email)
        user_obj.set_password(password)
        user_obj.email = email
        user_obj.firstname = firstname
        user_obj.lastname = lastname
        user_obj.profile_photo = profile_photo
        user_obj.mobile_phone_number = mobile_phone_number
        user_obj.createdAccount = django.utils.timezone.datetime.now
        user_obj.staff = is_staff
        user_obj.admin = is_admin
        user_obj.active = is_active
        user_obj.moderator = is_moderator
        user_obj.save(using=self._db)

        return user_obj
    def create_staffuser(self, username, email, firstname, lastname,mobile_phone_number, password=None):
        user = self.create_user(
            username,
            email,
            firstname,
            lastname,
            mobile_phone_number,
            password=password,
            is_staff=True
        )
        return user    
    def create_moderator(self, username, email, firstname, lastname,mobile_phone_number, password=None):
        user = self.create_user(
            username,
            email,
            firstname,
            lastname,
            mobile_phone_number,
            password=password,
            is_staff=True,
            is_admin=True,
            is_moderator=True
        )
        return user

    def create_superuser(self, username, email, firstname, lastname,mobile_phone_number, password=None):
        user = self.create_user(
            username,
            email,
            firstname,
            lastname,
            mobile_phone_number,
            password=password,
            is_staff=True,
            is_admin=True,
            is_moderator=True
        )
        return user      
class User(AbstractBaseUser):
    username = models.CharField(max_length=200, unique=True)
    firstname = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    mobile_phone_number = models.CharField(max_length=20, unique=True)
    profile_photo = models.ImageField(default='media/default.png', upload_to = user_directory_path)
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)
    moderator = models.BooleanField(default=False)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['firstname','lastname','email','mobile_phone_number']
    objects = UserManager()

    def __str__(self):
        return self.username
    def has_perm(self, perm, obj=None):
        return True
    def has_module_perms(self, app_label):
        return True    
    @property
    def is_staff(self):
        return self.staff
    @property
    def is_active(self):
        return self.active
    @property
    def is_admin(self):
        return self.admin
    @property
    def is_moderator(self):
        return self.moderator    

我试图找到解决方案,但没有找到解决办法。

0 个答案:

没有答案