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

时间:2018-05-30 12:39:13

标签: python django

我热心学习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中,它声称

  

“””   即使django.contrib.auth是,该模块也允许导入AbstractBaseUser   不在INSTALLED_APPS。“”“

掌握它的想法是我所不知道的。

以这种方式设计模块有什么好处?

1 个答案:

答案 0 :(得分:2)

task.status() 可以成为回答这类问题的有用工具。

如果您对base_user.py进行git责备,则可以看到评论已添加到this commit中,ticket 24564属于{{3}}。

如该故障单所述,如果您要从git blame导入AbstractBaseUser,则必须将django.contrib.auth.models添加到django.contrib.auth。这是因为该模块包含非抽象模型,如INSTALLED_APPS

因此,它被移动到一个单独的模块,允许导入它而不将User添加到django.contrib.auth

相关问题