gpt4 book ai didi

python - 自定义用户模型的问题

转载 作者:行者123 更新时间:2023-12-01 05:04:57 25 4
gpt4 key购买 nike

我半年前只完成了一个教程,这是我从那时起的第一个代码,所以请原谅我知识的缺乏。我试图通过在 Google 中搜索信息来使此代码正常工作,但我陷入了困境。

from django_uuid_pk.fields import UUIDField
from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.contrib.auth.models import BaseUserManager
from django.core.exceptions import ObjectDoesNotExist


class Avatar(models.Model):
first_name = models.CharField(_('first name'), max_length=30)
last_name = models.CharField(_('last name'), max_length=30)
uuid = UUIDField(unique = True, primary_key = True)
class Meta:
unique_together = ('first_name', 'last_name',)

class CustomUserManager(BaseUserManager):

def _create_user(self, first_name, last_name, uuid, password, email,
is_staff, is_superuser, **extra_fields):
now = timezone.now()
email = self.normalize_email(email)
try:
avatar = Avatar.objects.get(pk=uuid)
except ObjectDoesNotExist:
avatar = Avatar.objects.create(uuid=uuid, first_name=first_name, last_name=last_name)

user = self.model(avatar=avatar, email=email, is_staff=is_staff, is_superuser=is_superuser, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, first_name, last_name, uuid, password=None, email=None, **extra_fields):
return self._create_user(first_name, last_name, uuid, password, email, False, False,
**extra_fields)

def create_superuser(self, first_name, last_name, uuid, password=None, email=None, **extra_fields):
return self._create_user(first_name, last_name, uuid, password, email, True, True,
**extra_fields)

class CustomUser(AbstractBaseUser, PermissionsMixin):
avatar = models.ForeignKey('Avatar', unique=True)
email = models.EmailField(_('email address'), max_length=254, blank=True)
is_staff = models.BooleanField(_('staff status'), default=False)
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

objects = CustomUserManager()

USERNAME_FIELD = 'avatar'

class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')

def get_short_name(self):
return self.avatar.first_name

当我尝试通过命令在 shell 中创建用户时:

CustomUser.objects.create_user('Philip', 'Philips', 'f86cff00-2076-11e4-8c21-0800200c9a66', '123456')

我收到错误:

IntegrityError: UNIQUE constraint failed: myproject_accounts_customuser.avatar_id

最佳答案

您在 CustomUser 模型中的 avatar 字段上有 unique=True。但在您的自定义管理器中您有以下代码:

    try:
avatar = Avatar.objects.get(pk=uuid)
except ObjectDoesNotExist:
avatar = Avatar.objects.create(uuid=uuid, first_name=first_name, last_name=last_name)

如果具有该 uuid 的头像存在,您将获得一个头像对象,django 将尝试使用该对象创建用户。但由于头像是唯一的,如果数据库中存在具有该头像的用户,您将收到 IntegrityError。如果头像对象已经存在,如果您需要独特的行为,您应该在这个地方引发自定义异常。如果没有,请删除 unique=True

关于python - 自定义用户模型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228484/

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