为什么AbstractBaseUser被放在一个单独的模块中?

2024-03-28 14:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我热衷于学习Django的资源,从底层建立我的技能。你知道吗

在用户模型中,它具有password属性。使用命令from django.contrib.auth.models import User导入模型

我检查了模块django/models.py,没有找到password = models.CharField(),但最终在django/base_user.py中找到了它

class AbstractBaseUser(models.Model):
    password = models.CharField(_('password'), max_length=128)
    last_login = models.DateTimeField(_('last login'), blank=True, null=True)

有趣的是,AbstractBaseUserBaseUserManager被封装为一个单独的模块,而不是合并到models.py中,即使代码只有139行长。你知道吗

base_user.py中,它声称

""" This module allows importing AbstractBaseUser even when django.contrib.auth is not in INSTALLED_APPS."""

我无法理解它的意思。你知道吗

这样设计模块有什么好处?你知道吗


Tags: 模块djangopy模型authtruebasemodels
1条回答
网友
1楼 · 发布于 2024-03-28 14:36:18

git blame可以成为回答此类问题的有用工具。你知道吗

如果您将^{}归咎于git,那么您可以看到注释是添加在this commit中的,它是ticket 24564的一部分。你知道吗

正如那张罚单所解释的,如果您想从django.contrib.auth.models导入AbstractBaseUser,您必须将django.contrib.auth添加到INSTALLED_APPS。这是因为模块包含非抽象模型,如User。你知道吗

因此,它被移动到一个单独的模块中,这样就可以导入它,而无需将django.contrib.auth添加到INSTALLED_APPS。你知道吗

相关问题 更多 >