赞
踩
(MyTest) E:\devops\MyTest>python manage.py startapp login
# login/model.py
from django.contrib.auth.models import AbstractUser
from django.db import models
# Create your models here.
class UserInfo(AbstractUser):
# 添加一个字段
wechat = models.CharField(verbose_name='微信', blank=True, null=True, max_length=32)
def __str__(self):
return self.username
将model注册到admin
# login/admin.py from django.contrib import admin # Register your models here. from django.contrib.auth.admin import UserAdmin #使用django自己的UserAdmin来注册 from django.utils.translation import gettext, gettext_lazy as _ from .models import UserInfo # 重写fieldsets class UserAdmin(UserAdmin): #重写fieldsets在admin后台加入自己新增的字段 fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'wechat')}), (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}), ) # 注册到admin admin.site.register(UserInfo, UserAdmin)
#MyTest/settings.py
INSTALLED_APPS = [
#…………
'login.apps.LoginConfig',
#…………
]
AUTH_USER_MODEL = 'login.UserInfo'
# 初始化数据库
(MyTest) E:\devops\MyTest>python manage.py makemigrations
(MyTest) E:\devops\MyTest>python manage.py migrate
# 运行项目
(MyTest) E:\devops\MyTest>python manage.py makemigrations
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。