建立新專案,及應用
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