Django ValueError: Related model 'api.User' cannot be resolved
Django ValueError: Related model 'api.User' cannot be resolved
我将使用 django 自定义用户模型。写好代码后在db上迁移,出现如下错误
ValueError: Related model 'api.User' cannot be resolved
我的代码有什么问题?这是我的代码。
models.py
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
"""
A custom user manager to deal with emails as unique identifiers for auth
instead of usernames. The default that's used is "UserManager"
"""
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 Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', 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(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, null=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user can log into this site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
USERNAME_FIELD = 'email'
objects = UserManager()
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
class userDetail (models.Model) :
nickname = models.CharField(max_length=10)
profile = models.ImageField(default='media/default_image.jpeg')
而我在settings.pyAUTH_USER_MODEL = 'api.User'
也补充了。我该如何解决?谢谢。
要在 Django 中使用自定义用户模型,您需要确保以下几点:
api
是您的应用名称,
api
添加到 INSTALLED_APPS
列表中 settings.py
,
User
模型在 api
应用中
我在迁移具有 AbstractUser 的模型时遇到了同样的问题,我的用户模型配置是 AUTH_USER_MODEL = 'api.User'
通过迁移应用名称为 python manage.py migrate app_name
的模型解决了问题
我将使用 django 自定义用户模型。写好代码后在db上迁移,出现如下错误
ValueError: Related model 'api.User' cannot be resolved
我的代码有什么问题?这是我的代码。
models.py
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
"""
A custom user manager to deal with emails as unique identifiers for auth
instead of usernames. The default that's used is "UserManager"
"""
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 Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', 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(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, null=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user can log into this site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
USERNAME_FIELD = 'email'
objects = UserManager()
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
class userDetail (models.Model) :
nickname = models.CharField(max_length=10)
profile = models.ImageField(default='media/default_image.jpeg')
而我在settings.pyAUTH_USER_MODEL = 'api.User'
也补充了。我该如何解决?谢谢。
要在 Django 中使用自定义用户模型,您需要确保以下几点:
api
是您的应用名称,api
添加到INSTALLED_APPS
列表中settings.py
,User
模型在api
应用中
我在迁移具有 AbstractUser 的模型时遇到了同样的问题,我的用户模型配置是 AUTH_USER_MODEL = 'api.User'
通过迁移应用名称为 python manage.py migrate app_name