gpt4 book ai didi

python - Django 抽象用户模型未迁移

转载 作者:太空宇宙 更新时间:2023-11-03 14:19:51 26 4
gpt4 key购买 nike

所以我正在努力实现自定义用户模型,为此我遵循了教程“this”教程。

这些是模型:

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)
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):
"""User model."""

username = None
email = EmailField(_('email address'), unique=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

objects = UserManager()

我现在尝试运行 python3 manage.py migrate 并收到此错误:

[wtreston] ~/gdrive/mysite2/ $ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, reviews, sessions, users
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 164, in handle
pre_migrate_apps = pre_migrate_state.apps
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 218, in apps
return StateApps(self.real_apps, self.models)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 295, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field reviews.Answer.student was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field reviews.ReviewBlock.teacher was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field users.Student.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.
The field users.Teacher.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'.

在我的settings.py中我有这一行AUTH_USER_MODEL = 'users.User'
。当我使用ForeignKeys引用AUTH_USER_MODEL时,我首先有from mysite import settings,然后有ForeignKey(settings.AUTH_USER_MODEL)

任何帮助都会有帮助!谢谢

最佳答案

您已为自定义用户模型继承了 AbstractUser,并且您正在为 UserManager 继承了 BaseUserManager通过继承 UserManager 修改您的管理器,如下所示:

class MyUserManager(UserManager):
def create_user(self, email, password=None, **kwargs):
# override this method

def create_superuser(self, email, password, **kwargs):
# override this method

首先清理迁移和数据库,然后应用此更改进行迁移。

关于python - Django 抽象用户模型未迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48009764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com