# 关于设置AUTH_USER_MODEL出现的问题
关于设置AUTH_USER_MODEL出现的问题
在运行的时候出现了一个bug:
AttributeError: type object ‘UserProfile’ has no attribute 'USERNAME_FIELD’
网上提供的解决方案是:在user.models里面添加:
Django重写用户模型报错has no attribute 'USERNAME_FIELD'
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
但这样出现的问题是:identifier不是我们要设置的量。同时在创建超级用户时,添加完成identifier就出现警告!
在寻找解决方案发现,缺少了
http://www.it1352.com/636287.html
objects = UserManager()
运行之后,虽然可以设置密码了,但是邮箱设置,昵称设置依然无效;
以下代码是通过查找“USERNAME_FIELD”关键字,在程序里面找到的代码;
class AbstractUser(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Username and password are required. Other fields are optional.
"""
username_validator = UnicodeUsernameValidator()
username = models.CharField(
_('username'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=150, blank=True)
email = models.EmailField(_('email address'), blank=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user can log into this admin site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = UserManager()
EMAIL_FIELD = 'email'
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = True
def clean(self):
super().clean()
self.email = self.__class__.objects.normalize_email(self.email)
def get_full_name(self):
"""
Return the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
"""Return the short name for the user."""
return self.first_name
def email_user(self, subject, message, from_email=None, **kwargs):
"""Send an email to this user."""
send_mail(subject, message, from_email, [self.email], **kwargs)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
问题找到,添加的类出错。在视频讲解中继承的类是“AbstractUser”,而我使用的类是“AbstractBaseUser”。继承出错了!
# 关于设置AUTH_USER_MODEL出现的问题的更多相关文章
- Django auth认证
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...
- django用户认证系统——拓展 User 模型
Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...
- Django的认证系统
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...
- Django之自带的认证系统 auth模块
01-Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Dj ...
- Django自带的用户认证auth模块
一.介绍 基本上在任何网站上,都无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. 使用Django,我们可以不需要自己写这些功能,因为Dj ...
- Django之auth模块
http://www.cnblogs.com/liwenzhou/p/9030211.html 1.首先导入auth模块 from django.contrib import auth 2.创建aut ...
- Python-Django-Ajax进阶2
-forms组件的渲染错误信息 在模板中:<span>{{ foo.errors.0 }}</span> -forms使用bootstrap样式 widget=widgets. ...
- cnblog项目--20190309
第一个真正意义的Django项目 ! 预计时间5天 20190309--20190314 目标:学会Django的使用,理解模块关系! querset 相当于一个存放列表的字典 day ...
- REST framework---基于类的视图
一.程序设计 1.路由设计 from django.conf.urls import url from django.contrib import admin from app import view ...
随机推荐
- Codeforces 965 D. Single-use Stones(思维)
Codeforces 965 D. Single-use Stones 题目大意: 有一条河宽度为w,河上有一些石头,给出一组数(编号1~w-1),其中a[i]代表与河一岸距离为i的石头数量.每只青蛙 ...
- Latex里面的\newtheorem*{xx}{yy}后面的*是干什么的?
答案: 加了*表示不标号.例如: 同样使用命令: \begin{prb} xx\end{prb} 1. \newtheorem{prb}{Problem Formulation} → Problem ...
- 「CF525D」Arthur and Walls
题目链接 戳我 \(Solution\) 如果一个#要更改,那么一个四个格子的正方形只有他一个是#,bfs弄一下就好了 \(Code\) #include<bits/stdc++.h> u ...
- iPhone/iPad调整事件递交
UIKit 为应用程序提供了编程手段来简化事件处理或者完全关闭事件流.下面的列表总结了这些方法: 关闭触摸事件的递交. 缺省情况下,视图接收触摸事件,但是你可以设置它的userInteractionE ...
- Nginx之监控进程和工作进程
1. 函数调用分析 在开启 master 的情况下,多进程模型的下的入口函数为 ngx_master_process_cycle,如下: int mian() { ... if (ngx_proces ...
- vue 按需加载,缓存,导航守卫
开发中的注意事项:代码性能的优化 1. 减少对第三方的依赖,降低耦合度 2. 加强组件的重复利用率 3. 按需加载 4. 缓存 (尽量发送请求后保存数据) 5. 开发过程中,尽量有着面向对象的思想,这 ...
- Omnigraffle
OmniGraffle 7 Mac 注册码 账号:Appked 密码:MFWG-GHEB-HYTW-CGHT-CSXU-QCNC-SXU https://blog.csdn.net/ChibiMaru ...
- LC 644. Maximum Average Subarray II 【lock,hard】
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- sptringboot2.0实现aop
题记:在项目需要对请求日志情形管理. 声明:参考博客https://blog.csdn.net/bombsklk/article/details/79143145 1.在pom.xml中加入依赖 &l ...
- (“(null)” is of a model that is not supported by this version of Xcode. Please...)
真机测试遇到以下问题: (还以为手机不支持Xcode的版本呢) 解决方法: 发现只要将XCode重启后就可以真机运行了,碰见这个问题的朋友可以试下,我反正是被坑了半小时...