我创建了自定义用户模型。在进行迁移时,我得到了一个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()
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management__init __。py",第355行,执行中 self.fetch_command(子命令).run_from_argv(self.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行in执行 self.check()
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management \ base.py",第359行,in校验 include_deployment_checks = include_deployment_checks,
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management \ base.py",346行in _run_checks return checks.run_checks(** kwargs)
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ checks \ registry.py",第81行,in 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行,in check_user_mod 埃尔 if isinstance(cls()。is_anonymous,MethodType):
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ contrib \ auth \ base_user.py",第68行,in的初始化 super(AbstractBaseUser,self)。 init (* args,** kwargs)
文件" C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ db \ models \ base.py",第557行,in的初始化 _setattr(self,field.attname,val)
AttributeError:无法设置属性
有人可以指出哪里出错了吗?
答案 0 :(得分:0)
user = self.models(
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
)
替换模型