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 ...
随机推荐
- [科普] CPU, GPU, TPU的区别
Google Cloud 原文链接:https://cloud.google.com/blog/products/ai-machine-learning/what-makes-tpus-fine-tu ...
- vue中render: h => h(App)的详细解释
2018年06月20日 10:54:32 H-L 阅读数 5369 render: h => h(App) 是下面内容的缩写: render: function (createEleme ...
- python-日常用法小记
1.判断是否是数字 math.isnan("a") 2.数学math math.log(x) 3.查看安装路径 import sys print sys.path 4.字符串与日期 ...
- flask-数据库 进阶
1. 级联操作 Cascade意为“级联操作”,就是在操作一个对象的同时,对相关的对象也执行某些操作.我们通过一个Post模型和Comment模型来演示级联操作,分别表示文章(帖子)和评论,两者为一对 ...
- tkmybatis逆向工程关于tinyint的玄学问题
数据库中存储的数据类型是tinyint(1) state tinyint(1) 状态0-未完成:1-待提交:2-待支付:3支付失败: 不为空 tinyint(1)存储的时候会存储下面长度的数字 但是在 ...
- python3笔记二十四:Mysql数据库操作命令
一:学习内容 Mysql操作命令:启动服务.停止服务.连接数据库.退出数据库.查看版本.显示当前时间.远程连接 数据库操作命令:创建数据库.删除数据库.切换数据库.查看当前选择的数据库 表操作命令:查 ...
- 一、基础篇--1.2Java集合-HashMap和HashTable的区别
HashMap和HashTable的区别 1.继承的父类不同,HashMap继承的是AbstractMap类,HashTable继承的是Dictionary类,不过都实现了Map.Clone.Seri ...
- group_concat() 函数 拼接字符串长度有限制
最近,在做一个行转列的存储过程,遇到一个问题,问题如下: 我用group_concat()函数 来整合一个月每天的操作量,并将每天的操作量用CONCAT()函数拼接成 “MAX(IF(t.a = '2 ...
- StringJoiner 源码阅读
StringJoiner 属性说明 /** * StringJoiner 使用指定的分割符将多个字符串进行拼接,并可指定前缀和后缀 * * @see java.util.stream.Collecto ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...