django扩展用户继承AbstractUser

在app01/models.py里面创建自定义User

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

class User(AbstractUser):
    telephone = models.CharField(max_length=11, unique=True)
    school = models.CharField(max_length=100)

    USERNAME_FIELD = "telephone"   #USERNAME_FIELD作用,是执行authenticate验证, username参数传入后,实际校验的是telephone字段

在settings.py里面,告诉django不再使用默认的User,使用自定义的User

AUTH_USER_MODEL = 'app01.User'

执行makemigrations和migrate

manage.py@untitled1019 > makemigrations
"D:\Program Files\PyCharm 2018.1.4\bin\runnerw.exe" "D:\Program Files\python3.6.7\python.exe" "D:\Program Files\PyCharm 2018.1.4\helpers\pycharm\django_manage.py" makemigrations D:/pythonWorkspace/untitled1019
Tracking file by folder pattern:  migrations
Migrations for 'app01':
  app01\migrations\0001_initial.py
manage.py@untitled1019 > migrate
"D:\Program Files\PyCharm 2018.1.4\bin\runnerw.exe" "D:\Program Files\python3.6.7\python.exe" "D:\Program Files\PyCharm 2018.1.4\helpers\pycharm\django_manage.py" migrate D:/pythonWorkspace/untitled1019
Tracking file by folder pattern:  migrations
Operations to perform:
  Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying app01.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying sessions.0001_initial... OK

Process finished with exit code 0

同步完数据库,可以看见 app01_user表里多了两个字段, telephone、school

 

 dddd

猜你喜欢

转载自www.cnblogs.com/harryTree/p/11803678.html
今日推荐