django 2.0 扩展用户字段 示例

创建新项目,及应用

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
    原文作者:李毅
    原文地址: https://segmentfault.com/a/1190000015316799
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞