Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息。对于 Django 内置的 User 模型, 仅包含以下一些主要的属性:
- username,即用户名
- password,密码
- email,邮箱
- first_name,名
- last_name,姓
对于一些网站来说,用户可能还包含有昵称、头像、个性签名等等其它属性,因此仅仅使用 Django 内置的 User 模型是不够。好在 Django 用户系统遵循可拓展的设计原则,我们可以方便地拓展 User 模型。
继承 AbstractUser 拓展用户模型
这是推荐做法。事实上,查看 User 模型的源码就知道,User 也是继承自 AbstractUser 抽象基类,而且仅仅就是继承了 AbstractUser,没有对 AbstractUser 做任何的拓展。以下就是 User 的源码:
class User(AbstractUser):
"""
Users within the Django authentication system are represented by this
model.
Username, password and email are required. Other fields are optional.
"""
class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
所以,如果我们继承 AbstractUser,将获得 User 的全部特性,而且还可以根据自己的需求进行拓展。
我们之前新建了一个 users 应用,通常我们把和数据库模型相关的代码写在 models.py 文件里。打开 users/models.py 文件,写上我们自定义的用户模型代码:
users/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
nickname = models.CharField(max_length=50, blank=True)
class Meta(AbstractUser.Meta):
pass
我们给自定义的用户模型新增了一个 nickname(昵称)属性,用来记录用户的昵称信息,设置 blank=True 的目的是让用户在注册时无需填写昵称。根据你的需求可以自己进一步拓展,例如增加用户头像、个性签名等等,添加多少属性字段没有任何限制。
同时,我们继承了 AbstractUser 的内部类属性 Meta ,不过目前什么也没做。在这里继承 Meta 的原因是在你的项目中可能需要设置一些 Meta 类的属性值,不要忘记继承 AbstractUser.Meta 中已有的属性。
注意:一定要继承 AbstractUser,而不是继承 auth.User。尽管 auth.User 继承自 AbstractUser 且并没有对其进行任何额外拓展,但 AbstractUser 是一个抽象类,而 auth.User 不是。如果你继承了 auth.User 类,这会变成多表继承,在目前的情况下这种继承方式是不被推荐的。关于 Django 的抽象模型类和多表继承,请查阅 Django 的官方文档 模型继承。
此外,AbstractUser 类又继承自 AbstractBaseUser,前者在后者的基础上拓展了一套用户权限(Permission)系统。因此如非特殊需要,尽量不要从 AbstractBaseUser 拓展,否则你需要做更多的额外工作。
为了让 Django 用户认证系统使用我们自定义的用户模型,必须在 settings.py 里通过 AUTH_USER_MODEL 指定自定义用户模型所在的位置,即需要如下设置:
django_auth_example/settings.py
# 其它设置...
AUTH_USER_MODEL = 'users.User'
即告诉 Django,使用 users 应用下的 User 用户模型。
顺便再修改一下语言设置和时区设置:
django_auth_example/settings.py
# 其它设置...
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
设置好自定义用户模型后,生成数据库迁移文件,并且迁移数据库以生成各个应用必要的数据库表。即运行如下两条命令:
$ python manage.py makemigrations
$ python manage.py migrate
OK,现在 Django 用户系统使用的用户模型就是自定义的 User 模型了。
注意:一定要在设置好 AUTH_USER_MODEL = 'users.User' 后在第一次迁移数据库,即指定好自定义的用户模型后再执行数据库迁移命令。
使用 Profile 模式拓展用户模型
如果想为一个已使用了 Django 内置 User 模型的项目拓展用户模型,上述继承 AbstractUser 的拓展方式会变得有点麻烦。Django 没有提供一套自动化的方式将内置的 User 迁移到自定义的用户模型,因为 Django 已经为内置的 User 模型生成了相关数据库迁移文件和数据库表。如果非要这么做的话,需要手工修改迁移文件和数据库表,并且移动数据库中相关的用户数据。
所以我们采用另一种不改动数据库表的方式来拓展用户模型,具体来说,我们在创建一个模型(通常命名为 Profile)来记录用户相关的数据,然后使用一对一的方式将这个 Profile 模型和 User 关联起来,就好像每个用户都关联着一张记录个人资料的表一样。代码如下:
models.py
from django.contrib.auth.models import User
class Profile(models.Model):
nickname = models.CharField(max_length=50, blank=True)
user = models.OneToOneField(User)
这种方式和
AbstractUser 的区别是,继承 AbstractUser 的用户模型只有一张数据库表。而 Profile 这种模式有两张表,一张是 User 模型对应的表,一张是 Profile 模型对应的表,两张表通过一对一的关系关联。可见,当要查询某个用户的 Profile 时,需要执行额外的跨表查询操作,所以这种方式比起直接继承 AbstractUser 效率更低一点。因此对于新项目来说,优先推荐使用继承 AbstractUser 的方式来拓展用户模型。
PS:如果你使用了Profile 模式,你可能希望在创建 User 对象的时候同时也创建与之关联的 Profile 对象。你可以使用 Django 的 Signal 实现这个需求。由于 Profile 模式不是我们要介绍的重点内容,因此具体的实现细节请参照相关的文档,这里不再赘述。
OK,自定义的 User 模型已经建立好了,接下来就是如何创建用户,即用户注册流程了。
总结
本教程的示例项目代码位于 GitHub:Django Auth Example。
如果遇到问题,请通过下面的方式寻求帮助。
- 在下方评论区留言。
- 将问题的详细描述通过邮件发送到 djangostudyteam@163.com,一般会在 24 小时内回复。
- 在 Pythonzhcn 社区的新手问答版块 发布帖子。
更多 Django 相关教程,请访问我的个人博客:追梦人物的博客。
-- EOF --
第一个方法中,user表继承了AbstractUser
并且在类中填写了
那系统自带的swappable = 'AUTH_USER_MODEL'这个属性继承了吗?
这个属性是做什么用的?
为什么注册的页面没有显示nikename?
注册表单并没有 nickname 字段
好的谢谢,我后来搞明白了,在fields那里。
‘’‘OperationalError at /no such table: blog_postRequest Method:GETRequest URL:http://127.0.0.1:8000/Django Version:2.1.7Exception Type:OperationalErrorException Value:no such table: blog_postException Location:C:\LLL\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 298Python Executable:C:\LLL\Anaconda3\python.exePython Version:3.7.1Python Path:['C:\\LLL\\code\\django\\openlab', 'C:\\LLL\\Anaconda3\\python37.zip', 'C:\\LLL\\Anaconda3\\DLLs', 'C:\\LLL\\Anaconda3\\lib', 'C:\\LLL\\Anaconda3', 'C:\\LLL\\Anaconda3\\lib\\site-packages', 'C:\\LLL\\Anaconda3\\lib\\site-packages\\win32', 'C:\\LLL\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\LLL\\Anaconda3\\lib\\site-packages\\Pythonwin']
’‘’
博主你好 我是在前面的博客教程上注册了users
然后我也尝试的删除了文件目录里的数据库等生成文件
但是打开网站后报这个错
求帮助一下啊
为什么我从github上下载好源码后,点击注册或者登入,但是没又页面的跳转
File "C:\Users\Liukai\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\checks.py",line 29, in check_user_model if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):AttributeError: type object 'Employee' has no attribute 'REQUIRED_FIELDS'
这个报错 怎么解决呢????
知道问题了
什么问题??
我的问题可能有些复杂,我想让之前博客的评论功能中评论用户的名字不是自定义,而是登录用户才能评论。于是我将评论表单中名字,邮箱,URL字段都删除了,只留下像这样:
fields = ['text']
然后我再comments的models中将name字段设置成了外键,我是这样设置的,感觉问题是出在这一步:
name = models.ForeignKey('users.User',on_delete=models.CASCADE)
最后我在detail模板中的模板变量未做改变(我觉得好像不用变),依然是:
<span class="nickname">{{ comment.name }}</span>
但在提交评论是有一个NOT NULL constraint failed: comments_comment.name_id的错误,看字面是说这个name字段为空,就是没get到当前登录用户的名字,这是为什么?该怎么做?
成功了,在comments.views.py中为name字段赋值即可
c = Comment(name=request.user,text=comment,article=article)
c.save()
Unknown column 'comments_comment.name_id' in 'field list' 我这边提示,求解
可以了在comments的models定义name为外键关键(comments和User)
name = models.ForeignKey('users.User', on_delete=models.CASCADE)
注意上面外键,数据库comments表会生成name_id字段。
再修改forms.py的fields字段
fields = ['text'] #这里自己可以加email等其它,需要去掉name
最后修改comments的views视图函数
comment.name = request.user #新增这条
comment.save()
完成以上即可。
老哥,能加你QQ吗,我想知道你是怎么实现的。博主这个profile模式没讲详细
博主您好!为什么我用注册页面的用户名去登录老显示错误啊![Image]()
图片看不到了!是什么错误?
博主你好,感谢您昨天的解答。我已经解决了。但是今天我尝试在另一个项目中加入登录系统出现许多的错误(使用的是您另一个blog搭建教程实例),因为之前已经在models中注册了数据库,使用的是MySQL,已经成功。现在想加入注册、登录、注销等功能,但是我卡在models里面了。报错:
blog.Post.author: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out. HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
我在setting里面已将加入AUTH_USER_MODEL = 'blog.User'。也注册了app,也尝试下面朋友报错您给出的方案:
所有通过 foreinkey 关联 user 的都要使用 settings.AUTH_USER_MODEL
例如:class Post(models.model):author = models.ForeignKey(settings.AUTH_USER_MODEL)
但是我使用了也不行,会报错。也尝试了from django.conf import settings,但根本没用。请问博主,这个问题该怎样解决?我还等着部署在服务器呢!
麻烦好心的博主大大,帮忙解答下呗。万分感谢!
如果你以前关联了 auth.User,那改为自定义 User 后需要删掉数据库重新生成。
请问博主,怎样操作呢?
修改代码后删掉数据库和 migratons/ 文件下的全部文件,然后重新运行迁移数据库的两条命令即可。
博主,您好。我是按照您这个方式做的。但是得到如下错误,是怎么回事呢?
Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\core\management\__init__.py", line 364, in execute_from_command_line utility.execute() File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\core\management\__init__.py", line 338, in execute django.setup() File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\apps\registry.py", line 116, in populate app_config.ready() File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\contrib\admin\apps.py", line 23, in ready self.module.autodiscover() File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\contrib\admin\__init__.py", line 26, in autodiscover autodiscover_modules('admin', register_to=site) File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\utils\module_loading.py", line 50, in autodiscover_modules import_module('%s.%s' % (app_config.name, module_to_search)) File "E:\ruanjian\py\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 673, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 665, in exec_module File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\contrib\auth\admin.py", line 7, in <module> from django.contrib.auth.forms import ( File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\contrib\auth\forms.py", line 22, in <module> UserModel = get_user_model() File "E:\ruanjian\py\lib\site-packages\django-1.11.8-py3.5.egg\django\contrib\auth\__init__.py", line 198, in get_user_model "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODELdjango.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed
user 没有加入 INSTALLED_APPS 中,注意看报错信息的提示。
博主您好,请问执行makemigrations出现以下错误怎么回事呢,我settings里面也加了AUTH_USER_MODEL的
manage.py@blog_project > makemigrations "D:\软件目录\PyCharm 2017.2.3\bin\runnerw.exe" C:\Users\niejc\Envs\zmrc_blog_env\Scripts\python.exe "D:\软件目录\PyCharm 2017.2.3\helpers\pycharm\django_manage.py" makemigrations E:/items/blog_projectSystemCheckError: System check identified some issues:ERRORS:blog.Post.author: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out. HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'. 将所有指向 User 的关系改为 settings.AUTH_USER_MODEL
博主您好,想请问一下,我用的是MySql数据库,按照上面设置完之后,运行manage.py报了一个错误:Error fetching command 'createsuperuser': AUTH_USER_MODEL refers to model 'users.User' that has not been installed .
Command 'createsuperuser' skipped
这个是因为没用django自带的数据库导致的吗?(AUTH_USER_MODEL = 'users.User' 在settings.py中配置好了)
我弱智了。。忘记填写APP了
博主你好,我想请问下如果用Profile拓展了用户模型,要如何应用profile里面的数据?
我的models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
nickname = models.CharField(max_length=50, blank=True)
class Meta(AbstractUser.Meta): pass
class MyUser(models.Model):
user = models.OneToOneField(User)
phone = models.CharField(max_length=20, null=True, blank=True)
avatar = models.ImageField(upload_to='photo', null=True, blank=True)
我已经用admin账号在后台填写了phone的值了。
但是我在test.html里写{{user.username}}可以显示出来账号名字
写{{Myuser.phone}}却显示不出来电话,请问需要怎么做才能让前台显示出后台的数据呢?
刚刚开始学,如果问了蠢问题请见谅
你如果 user 是 User 的实例,应该这样:user.myuser.phone。否则你需要使用 MyUser 的实例直接获取{{myuser.phone}},属性属于哪个类,就要使用哪个类的实例,user.myuser 可以获得 user 关联的 MyUser 实例。
博主你好,我有遇到一个问题,我想要在视图函数中调用User中自带的属性,比如(username)。不知道应该怎么做,望告知。
对 User 是实例引用username即可,例如 user.username(假设你的 user 为你创建的 User实例)
您好,在拓展用户模型的时候解决中文翻译问题?比如说:
我添加了一个叫做mobile_phone_number的域来存储用户的手机号码。但是在渲染成注册界面的表单时,显示为“Mobiel phone number”。请教博主有没有方法可以将其显示为中文的“手机号码”?
提前谢谢您啦~
在 mobile_phone_number Field 里设置参数 verbose_name = ‘手机号’
按照您说的方法已经解决了。非常感谢博主!
博主我在你之前的django博客教程中按照你的注册教程加了user app 然后报错了:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.HINT: Add or change a related_name argument to the definition for 'User.groups' or'UserProfile.groups'.auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or'UserProfile.user_permissions'.
这能解决吗
把异常信息翻译成中文就可以得到解决方案。
博主,我出现了这个问题,请问该如何解决?
Operations to perform: Apply all migrations: admin, auth, contenttypes, polls, sessionsTraceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "D:\python3.6.2\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line utility.execute() File "D:\python3.6.2\lib\site-packages\django\core\management\__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\python3.6.2\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "D:\python3.6.2\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(*args, **options) File "D:\python3.6.2\lib\site-packages\django\core\management\commands\migrate.py", line 164, in handle pre_migrate_apps = pre_migrate_state.apps File "D:\python3.6.2\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "D:\python3.6.2\lib\site-packages\django\db\migrations\state.py", line 218, in apps return StateApps(self.real_apps, self.models) File "D:\python3.6.2\lib\site-packages\django\db\migrations\state.py", line 295, in __init__ raise ValueError("\n".join(error.msg for error in errors))ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'polls.user', but app 'polls' doesn't provide model 'user'.
from blogproject.settings import AUTH_USER_MODEL
问题解决
嗯,我好像在文中没有说明如何关联自定义 User 的问题,你的方法是对的,不过对于 settings 对象,最好从
博主,我遇到了和楼下的楼下 同一个问题,这是我的解决方案
from blogproject import settings
…………
author = models.ForeignKey(settings.AUTH_USER_MODEL)
但是报错AttributeError: 'module' object has no attribute 'settings’
我导入的settings不对吗?那请问应该怎么弄?
migrate的时候报错,希望能帮忙看下,谢谢!
莫名其妙地又弄好了.....
偶然问题,重新做一次就好了。
blog.Post.author: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
继承AbstractUser 在生成数据库时报了这个错
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'. 它提示你了。所有通过 foreinkey 关联 user 的都要使用 settings.AUTH_USER_MODEL
关键是我在setting里面配置了这个才报的这个错
所有通过 foreinkey 关联 user 的都要使用 settings.AUTH_USER_MODEL
例如:
class Post(models.model):
author = models.ForeignKey(settings.AUTH_USER_MODEL)
详细信息可以参考:https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#referencing-the-user-model
好的,我去看看,谢谢博主
试试
ModuleNotFoundError: No module named 'user'
这个错误是因为没有自定义好吗?但是我的settings,models都检查了一遍没问题啊。
应该是 user 路径问题,确保 user 符合 python package 规范,并且把 user 加入到 settings 的应用列表里。
users跟manage.py同一个目录,settings中也添加了AUTH_USER_MODEL='users.User',apps中也添加了,
解决了,urls中include(‘users.urls’)写成user.urls
楼主 前后台用户 您能给指导一下是怎么分离的吗
我还没实践过,不过方案应该很多,最好的就是用 django-rest-framework + 一个前端框架如 react.js/vue.js 等实现前后端分离。
博主你好,我的项目是用Pycharm生成的,里面的AUTH_USER_MODEL = 'users.User'这个是在global.settings里面的,修改时提醒这个文件不属于这个项目,我自己的理解是不能够修改。但是在项目的settings文件中又找不到AUTH_USER_MODEl配置,请问这种情况下该如何解决啊?谢谢。
在新生成的 django 项目下应该有一个 settings.py 文件吧,和 urls.py、wsgi.py 一个目录的,在这个文件里添加即可。
好的,谢谢博主。
博主,直接添加的话出现了一堆报错
Traceback (most recent call last):
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth_init_.py", line 193, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py", line 200, in get_model
app_config = self.get_app_config(app_label)
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py", line 156, in get_app_config
raise LookupError(message)
LookupError: No installed app with label 'user'.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):", line 978, in _gcd_import", line 961, in _find_and_load", line 950, in _find_and_load_unlocked", line 655, in _load_unlocked", line 678, in exec_module", line 205, in _call_with_frames_removed .py", line 198, in get_user_model
File "C:\Program Files\JetBrains\PyCharm 2017.1.4\helpers\pycharm_jb_manage_tasks_provider.py", line 25, in
django.setup()
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django_init_.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.ready()
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\apps.py", line 23, in ready
self.module.autodiscover()
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin_init_.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\importlib_init_.py", line 126, in import_module
return bootstrap._gcd_import(name[level:], package, level)
File "
File "
File "
File "
File "
File "
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\admin.py", line 7, in
from django.contrib.auth.forms import (
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\forms.py", line 22, in
UserModel = get_user_model()
File "C:\Users\jingj\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth_init
"AUTH_USER_MODEL refers to model '%s' that has n
哦哦,好了,有个拼写错误。
看看你的目录结构以及相关的代码。
博主你好。想请问下,基本按照博主的教程敲得代码,在注册表单提交的时候,报错。没有users_user这张表,麻烦请问下是什么原因。谢谢。
需要运行 python manage.py makemigrations 和 migrate 创建数据库哈。
运行了这两句,就是报错 no such table:users_user.
我怀疑你是否漏掉了一些步骤,例如配置 settings,详细内容见第一篇教程。
就是创建数据库的时候,没反应,no changes.我在models添加删除都是 no changes
现在解决了,谢谢博主的耐心解答。python manage.py makemigrations users
使用了这条语句就新建了 users_usrer表
另外如果我有两种不同的用户类型有不同的属性,比方说学生和老师, 是应该用OneToOneField吗?如果AbstractUser的话好像有点复杂(先写一个继承AbstractUser的类,然后学生和老师分别前面那个类),而且这种写法好像也有点问题?谢谢。
学生和老师分别继承前面那个类
这种场景比较推荐使用 profile 模式。
请问一下博主,用AbstractUser方法的话,是否应该还要再定义一个user manager? 否则无法创建superuser?
不用,这个类已经有了一个 manager,有 create user 和 superuser 的方法。