Django用户认证模块中继承AbstractUser与AbstractBaseUser重写User表的区别

AbstractUser和AbstractBaseUser看起来十分相似,如果你不熟悉djiango的auth重写User,那你很容易弄错,导致一堆bug。

 

 

 

我们查看AbstractUser的源码得知,AbstractUser继承了AbstractBaseUser,讲得俗气一点就是,AbstractBaseUser是AbstractUser的爸爸。

 

 

我们可以猜想一下,既然二者是继承与被继承关系,那么AbstractUser是不是在AbstractBaseUser的基础上功能更加完善呢?AbstractBaseUser是不是更加open呢?

通过官方文档我们可以得到答案:

AbstractUser

         The documentation explains this fully. AbstractUser is a full User model, complete with fields, as an abstract class so that you can inherit from it and add your own profile fields and methods. AbstractBaseUser only contains the authentication functionality, but no actual fields: you have to supply them when you subclass.

文档充分解释了这一点。 AbstractUser是一个完整的用户模型,包含字段,作为一个抽象类,以便您可以继承它并添加您自己的配置文件字段和方法。 AbstractBaseUser仅包含身份验证功能,但不包含实际字段:当您继承子类时,您必须提供它们。

         The AbstractUser is basically just the "User" class you're probably already used to. AbstractBaseUser makes fewer assumptions and you have to tell it what field represents the username, what fields are required, and how to manage those users.

AbstractUser基本上就是您可能已经习惯的“用户”类。 AbstractBaseUser的继承较少,您必须告诉它哪个字段代表用户名,需要哪些字段以及如何管理这些用户。

AbstractBaseUser           

            If you're just adding things to the existing user (i.e. profile data with extra fields), then use AbstractUser because it's simpler and easier. If you want to rethink some of Django's assumptions about authentication, then AbstractBaseUser gives you the power to do so.

如果您只是将事情添加到现有用户(即具有额外字段的配置文件数据),则使用AbstractUser是因为它更简单,更简单。 如果您想重新考虑一下Django关于认证的假设,那么AbstractBaseUser会为您提供这样的权力。

什么意思呢?就是说啊,我们习惯的继承 的AbstractUser 类是高度集成的,里面给你定义了一堆的字段,不需要你人为去定义了。

上面是我们需要额外添加的,下面是django帮你额外做的(没有显示完全,右边还有自己添加的部分字段)

 

 

 

但回过头来想,高度集成的东西往往扩展性和兼容性就较差,万一哪天一个项目来了说我只需要基本的用户名密码,用户类型等等三四个字段,其他的都不care,那么很显然这时候用AbstractUser  是不合理的,将造成数据库资源的浪费,降低数据库效率。

这时候我们就可以来继承AbstractBaseUser  类来自定义一些字段。下面我们来看看AbstractBaseUser  的用法

 model

创建后的所有表字段

 

 由此可见,django只帮我们额外创建了id、password、last_login这三个字段。

在模型类中我们必须定义一个用户名字段,并指定属性为unique,然后告诉django这个字段是用户名字段:

    username = models.CharField(max_length=32,unique=True)
    USERNAME_FIELD = 'username'

    # 这当中的username你可以任意命名,unique必须指定为True

如果不写这两句话,你会发现执行数据库迁移命令怎么创建表都没办法创建出来,一直报错:

AttributeError: type object 'UserInfo' has no attribute 'USERNAME_FIELD'

 

开放的东西往往也意味着更加纯净,那么这里就会有一些问题值得注意,当使用AbstractBaseUser 的时候我们需要注意的是:如果你使用了AbstractBaseUser  ,那么django自带的auth认证的所有用法将统统不能使用,你需要自己去写加密、登陆判断、存储等等一系列方法。 

 

 

 

如果你要删库重新建model,请到你的app下面的migrations文件夹下面把除__init__.py的其他文件全部删除,再执行数据库迁移命令。

顺带把数据库迁移命令语句丢在这儿:

第一种方式:PyCharm的Terminal命令行:

第一条:python  manage.py makemigrations  或者 python3 manage.py makemigrations   ###根据你配置的python环境而定

第二条:python manage.py migrate    或者 python3 manage.py migrate

 

 

 第二种方式:PyCharm上菜单栏Tools --> run manage.py Task...  

第一条:makemigrations 
第二条:migrate

 

此外自定义User表,如果希望django只生成我们自己定义的User表,不生成django自带的auth_user表,你需要导setting里加一行代码:

 

AUTH_USER_MODEL = '应用名.表名'

 

 

 

 

 觉得写得好,给个赞呗~~~~~~欢迎来搂~~

 

posted @ 2019-06-18 19:14  希希大队长  阅读(4186)  评论(1编辑  收藏  举报