django 2.0 擴充套件使用者欄位 示例

李毅發表於2018-06-19

建立新專案,及應用

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

相關文章