gpt4 book ai didi

django - 为什么初始迁移有依赖关系?

转载 作者:行者123 更新时间:2023-12-02 05:28:00 27 4
gpt4 key购买 nike

在没有任何先前迁移的情况下,我已在核心应用程序中添加了自定义用户模型,以按照文档条目 Django 2.0 / Customizing authentication in Django 启用自定义身份验证

然后我进行了迁移:

$ python manage.py makemigrations

Migrations for 'core':
core/migrations/0001_initial.py
- Create model User

0001_initial.py 迁移类开始于:

class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]

我查阅了官方文档Django 2.0 / Migrations这个文件显然是一个初始迁移,因为它有 initial = True。但为什么它有一整套以 0009_alter_user_last_name_max_length 开头的依赖链? ?我有什么错误吗?

运行迁移时,输出如下:

$ python manage.py migrate  
Operations to perform:
Apply all migrations: admin, auth, contenttypes, core, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying core.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying sessions.0001_initial... OK

当省略自定义User模型时,0001_initial.py开头为:

class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

此外,我无法找到 migrations.swappable_dependency(settings.AUTH_USER_MODEL) 确切用途的良好解释。它有什么作用以及为什么它被称为“swappable_dependency”?如果有人也能为我揭开这部分的神秘面纱,或者指出我自己探索的正确方向,那就太好了。 🙂

编辑#1

自定义用户模型的源代码。

class UserManager(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError('Email not defined.')

user = self.model(email=self.normalize_email(email))

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, password):
user = self.create_user(
email,
password=password,
)
user.is_staff = True
user.save(using=self._db)
return user


class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
created_at = models.DateTimeField(auto_now_add=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)

EMAIL_FIELD = 'email'
USERNAME_FIELD = 'email'

objects = UserManager()

def __str__(self):
return self.email

def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True

最佳答案

这是正常且预期的行为 - 您仍然需要将 django.contrib.auth 的迁移应用到您的数据库。

如果您look at those migrations 它们包括身份验证应用程序所需的各种其他模型 - 例如 PermissionGroupUser 模型具有引用这些其他模型的字段,因此您需要先进行迁移才能设置这些模型。

因为 User 是一个可交换的依赖项,Django 将忽略 auth 应用中的 User 迁移,并使用您的迁移。但所有其他 auth 模型的迁移将从 auth.migrations 应用。

正是由于对 auth 迁移的依赖,即 very difficult to change设置项目后的自定义用户模型:

Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.

关于django - 为什么初始迁移有依赖关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51084875/

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