在我的模型中,我创建了UserManager和User并将其添加到设置中:
=
但是当我迁移它会引发错误,无法解决与模型相关的问题。 我搜索了互联网,但找不到解决我问题的答案 <---> 由于在互联网上对于这个问题没有很好的答案,我仍然没有在这里的朋友的帮助下找到解决方案(所有这些我都不知道减分),我想我需要问更多细节 我在我的models.py中定义了此类:
AUTH_USER_MODEL = "myapp.User"
并且我在设置中添加了它:
from __future__ import unicode_literals
from django.db import models
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import (
AbstractBaseUser, PermissionsMixin
)
from django.contrib.auth.models import (
AbstractBaseUser, PermissionsMixin, BaseUserManager
)
class UserManager(BaseUserManager):
def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email,and password.
"""
if not email:
raise ValueError('The given email must be set')
try:
with transaction.atomic():
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
except:
raise
def create_user(self, email, password=None, **extra_fields):
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):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(email, password=password, **extra_fields)
class User(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
"""
email = models.EmailField(max_length=40, unique=True)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
def save(self, *args, **kwargs):
super(User, self).save(*args, **kwargs)
return self
我运行makemigrations,它没有错误,但是当我尝试运行迁移时,出现此错误:
AUTH_USER_MODEL = "myapp.User"
答案 0 :(得分:1)
我认为这个问题是您一直在使用django的用户模型,突然之间您将USER_AUTH_MODEL
更改为自定义User
模型。然后,您正在运行迁移,并且会引发错误。
实际上,没有很好的解决方案。请参阅documentation。
如文档中所述,for可以按照故障单25313中的说明迁移用户:
__init__.py
文件除外)python manage.py makemigrations
)python manage.py migrate --fake
)