配置SimpleUI
在setting.py中设置:
INSTALLED_APPS = [
# 在最前面加入
'simpleui',
'django.contrib.admin',
'django.contrib.auth',
...
]
# 中文语言
LANGUAGE_CODE = 'zh-Hans'
# 上海时区
TIME_ZONE = 'Asia/Shanghai'
重定义User模型
- 新建一个
user
应用
python manage.py startapp user
2.设置user
应用的 models.py
,添加重定义的User模型
from django.contrib.auth.models import AbstractUser # 抽象用户类
from django.db import models
from django.utils.translation import gettext_lazy
class UserGenderType(models.IntegerChoices):
MAN = 0, gettext_lazy('男')
WOMAN = 1, gettext_lazy('女')
UNKNOWN = 2, gettext_lazy('未知')
class LuckyUser(AbstractUser):
class Meta:
verbose_name_plural = '用户'
nickname = models.CharField(verbose_name='昵称', max_length=255, default='')
phone = models.CharField(verbose_name='手机', max_length=255, null=True, blank=True, default='')
gender = models.IntegerField(verbose_name='性别 :0男 1女 2未知', choices=UserGenderType.choices,
default=UserGenderType.UNKNOWN)
def __str__(self):
return self.nickname + '-id:' + str(self.id)
- 在
setting.py
里面:
# 设置User模型,user是应用名,LcukyUser是类名。
AUTH_USER_MODEL = 'user.LuckyUser'
- 配置
user
应用的admin.py
:
from django.contrib import admin
from user.models import *
@admin.register(LuckyUser)
class LuckyUserAdmin(admin.ModelAdmin):
model = LuckyUser
创建超级管理员账号
- 执行迁移脚本指令:
python manage.py makemigrations
python manage.py migrate
- 创建超级管理员账号并启动项目
python manage.py createsuperuser # 设置用户名、邮箱和密码
python manage.py runserver