我正在django中构建一个自定义User类以用于创建注册应用程序,并且每次尝试进行迁移时都会不断出现上述错误。据我所知,我的代码是按django文档进行的。也将AUTH_USER_MODEL正确放置在我的设置配置中。这是我的模型。py
# from __future__ import unicode_literals
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.validators import RegexValidator
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
if password:
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
email = models.EmailField(_('email address'), unique=True, default='email')
username = None
first_name = models.CharField(max_length=20, blank=False, null=False, default='first_name')
last_name = models.CharField(max_length=20, blank=False, null=False, default='last_name')
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+91 ...'")
phone_no = models.CharField(validators=[phone_regex], max_length=17,blank=False, default='phone_number')
# email_validator = EmailValidator(message='Invalid email
# address',code=None,whitelist=None)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
# Email & Password are required by default
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
objects = UserManager()
def get_full_name(self):
return self.email
def get_short_name():
return self.email
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
# does user have a specific permission
return True
def has_module_pers(self, app_label):
# does user have permissions to view the app 'app_label'
return True
@property
def is_admin(self):
return self.is_admin
@property
def is_active(self):
return self.is_active
# Create your models here.""
答案 0 :(得分:0)
我从数据库中删除了现有用户,并从迁移文件夹中删除了先前的迁移文件。再次解决迁移问题后