1.使用的AbstractUser
和AbstractBaseUser
看起来颇为相似。
from django.contrib.auth.models import AbstractUser, AbstractBaseUser
从django.contrib.auth.models导入AbstractUser,AbstractBaseUser
2.What is the difference between the two?
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的继承较少,您必须告诉它哪个字段代表用户名,需要哪些字段以及如何管理这些用户。
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会为您提供这样的权力。