我创建了我的自定义用户模型。在进行迁移时,我得到了一个 AtrributeError
from django.db import models
from time import timezone
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.mail import send_mail
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
class CustomUsermanager(BaseUserManager):
def _create_user(self, is_anonymous, first_name, last_name, email, username, password, home_address, user_type, image_path):
now = timezone.now()
if not email:
raise ValueError('The gives emial must be set')
email = self.normalize_email(email)
user = self.model(
is_anonymous=is_anonymous,
first_name=first_name,
last_name=last_name,
email=email,
username=username,
home_address=home_address,
user_type=user_type,
image_path=image_path,
created_time=now,
last_login=now
)
user.set_password(password)
user.save(using=self._db)
return user
def create_a_admin(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, password, home_address, 0, image_path)
def create_a_nonanonymous_patient(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 1, password, home_address, 1, image_path)
def create_an_anonymous_patient(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, 1, password, home_address, 1, image_path)
def create_a_nonanonymous_helper(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 2, password, home_address, 2, image_path)
def create_an_anonymous_helper(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(1, first_name, last_name, email, username, 2, password, home_address, 2, image_path)
def create_a_prof(self, first_name, last_name, email, username, password, home_address, image_path):
return self._create_user(0, first_name, last_name, email, username, 3, password, home_address, 3, image_path)
class CustomUser(AbstractBaseUser):
is_anonymous = models.BooleanField()
username = models.CharField(max_length=255, unique=True)
first_name = models.CharField(max_length=255, blank=True)
last_name = models.CharField(max_length=255, blank=True)
email = models.EmailField(blank=True, unique=True)
home_address = models.CharField(max_length=255, blank=True)
user_type = models.IntegerField(1)
image_path = models.CharField(max_length=500, blank=True)
created_time = models.TimeField()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['username', 'home_address', 'first_name', 'last_name', 'user_type']
objects = CustomUsermanager()
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_absolute_url(self):
return '/users/%s/' % urlquote(self.email)
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def get_email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
例外是:
回溯(最近一次通话最后):
文件“manage.py”,第 22 行,在 execute_from_command_line(sys.argv)
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py”,第 363 行,在 execute_from_command_line utility.execute()
在执行 self.fetch_command(subcommand).run_from_argv(自我.argv)
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py”,第 283 行,在 run_from_arg v self.execute(*args , **cmd_options)
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py”,第 327 行,在执行 self.check()
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py”,第 359 行,检查 include_deployment_checks=include_deployment_checks,
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py”,第 346 行,在 _run_checks 返回 checks.run_checks(**夸格斯)
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\registry.py”,第 81 行,在 run_checks new_errors = check(app_configs=app_configs )
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\checks.py”,第 77 行,在 check_user_model if isinstance(cls() .is_anonymous,方法类型):
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\base_user.py”,第 68 行,init super(AbstractBaseUser, self)。初始化(*args, **kwargs)
文件“C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\base.py”,第 557 行,在init _setattr(self, field.attname , 值)
AttributeError:无法设置属性
谁能指出哪里错了?