1

创建新项目,及应用

django-admin startproject myproj
cd myproj
python manage.py startapp myapp

自定义 User

文件myapp/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    name = models.CharField(blank=True, max_length=255)

文件 myproj/settings.py

INSTALLED_APPS = [
    ...
    'myapp',
]

AUTH_USER_MODEL = 'myapp.User'

文件 myproj/admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User


admin.site.register(User, UserAdmin)

更新数据库

python manage.py makemigrations myapp
python manage.py migrate
// python manage.py createsuperuser

李毅
5k 声望1.5k 粉丝

贈人玫瑰,手有餘香