Django重写用户模型报错has no attribute 'USERNAME_FIELD'
Django重写用户模型报错has no attribute 'USERNAME_FIELD'
在重写用户模型时报错:AttributeError: type object ‘UserProfile’ has no attribute ‘USERNAME_FIELD’
models.py
- 新建用户模型UserProfile继承自AbstractBaseUser
# -*- encoding:utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
# Create your models here.
class UserProfile(AbstractBaseUser):
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(max_length=5, choices=(("male", u"男"),("female",u"女")), default="female")
address = models.CharField(max_length=100, default=u"")
mobile = models.CharField(max_length=11, null=True, blank=True)
image = models.ImageField(upload_to="image/%Y/%m", default=u"image/default.png", max_length=100)
class Meta:
verbose_name = u"用户信息"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username1234567891011121314151617181920212223
settings.py中也设置了AUTH_USER_MODEL
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mx_users',
]
AUTH_USER_MODEL = "mx_users.UserProfile"123456789101112
debug时报错:
AttributeError: type object 'UserProfile' has no attribute 'USERNAME_FIELD'1
最后google了一圈才发现解决办法:
答案还是得去官方文档中寻找
https://docs.djangoproject.com/en/1.9/topics/auth/customizing/
在模型中新增两行代码,即可解决
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'12
如下:
class UserProfile(AbstractBaseUser):
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(max_length=5, choices=(("male", u"男"),("female",u"女")), default="female")
address = models.CharField(max_length=100, default=u"")
mobile = models.CharField(max_length=11, null=True, blank=True)
image = models.ImageField(upload_to="image/%Y/%m", default=u"image/default.png", max_length
Django重写用户模型报错has no attribute 'USERNAME_FIELD'的更多相关文章
- Django 重写用户模型
AUTH_USER_MODEL = 'myapp.MyUser' django——重写用户模型 Django内建的User模型可能不适合某些类型的项目.例如,在某些网站上使用邮件地址而不是用户名作为身 ...
- django重写用户模型
重写一个UserProfile继承自带的AbstractUser # -*- coding: utf-8 -*- from __future__ import unicode_literals fro ...
- 重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’
view中导入:from django.contrib.auth.models import AbstractBaseUser settings.py中设置了:AUTH_USER_MODEL='app ...
- django 重写用户模型 AbstractBaseUser
https://blog.csdn.net/weixin_40744265/article/details/80745652
- Django之重写用户模型
django——重写用户模型 Django内建的User模型可能不适合某些类型的项目.例如,在某些网站上使用邮件地址而不是用户名作为身份的标识可能更合理. 1.修改配置文件,覆盖默认的User模型 D ...
- django 学习手册 - ORM 报错集(随时更新)
报错问题: 问题一:(1050代码) django.db.utils.InternalError: (1050, "Table 'app01_group' already exists&qu ...
- Oracle存储过程跨用户执行查询报错
在Oracle中,在USERA下编写一个存储过程,该存储过程中引用了另一个用户USERB下的表或视图对象.编译该存储过程,出现编译错误.报ORA-00942: table or view does n ...
- django框架使用mysql报错,及两种解决方法
1.django框架 settings.py文件中部分代码: DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3' ...
- Jumpserver(跳板机、堡垒机)启动jms Django连接mysql数据库报错
解决办法 根据报错信息 , 去查看官方手册 在settings.py文件夹加入DATABASES['OPTIONS']['init_command'] = "SET sql_mode='ST ...
随机推荐
- Vue_(组件)过渡效果
Vue.js进入/离开 & 列表过渡 传送门 进入/离开过渡效果:Vue在插入.更新或移除DOM时,可以设置一些动画效果: 过渡效果中也提供了对应的钩子函数,这些钩子函数需要在<tran ...
- beta week 2/2 Scrum立会报告+燃尽图 07
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9959 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名: ...
- 【python / mxnet / gluoncv / jupyter notebook】变换场景的同一行人多重识别
程序环境为高性能集群:CPU:Intel Xeon Gold 6140 Processor * 2(共36核心)内存:512GB RAMGPU:Tesla P100-PCIE-16GB * 2 数 ...
- LeetCode 80. 删除排序数组中的重复项 II(Remove Duplicates from Sorted Array II)
题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- spark streaming 4: DStreamGraph JobScheduler
DStreamGraph有点像简洁版的DAG scheduler,负责根据某个时间间隔生成一序列JobSet,以及按照依赖关系序列化.这个类的inputStream和outputStream是最重要的 ...
- Python3并发写文件
使用python2在进行并发写的时候,发现文件会乱掉,就是某一行中间会插入其他行的内容. 但是在使用python3进行并发写的时候,无论是多进程,还是多线程,都没有出现这个问题,难道是python3的 ...
- mysql 查看库结构---查看表结构
查看库结构:SHOW DATABASES; 查看表结构: show tables即为显示当前数据库中所有的表.又如: mysql> use dbname //进入dbname库Database ...
- ccf 201409-3 字符串匹配(toupper,tolower)
ccf 201409-3 字符串匹配(toupper,tolower) 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一 ...
- 一个继承的 DataGridView
// 允许增加一个 checkbox 列 public class DgvBase : DataGridViewX { protected override void OnColumnAdded(Da ...
- LC 965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...