1. 程式人生 > >Django-元件--使用者認證Auth(auth_user增加欄位)

Django-元件--使用者認證Auth(auth_user增加欄位)

引入:

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

原始碼 :

from django.contrib.auth.models import User  
(user指的是 auth_user表)
User原始碼=====》
class User(AbstractUser):
    """
    Users within the Django authentication system are represented by this
    model.

    Username and password are required. Other fields are optional.
    
""" class Meta(AbstractUser.Meta): swappable = 'AUTH_USER_MODEL'

 

一:增加 auth_user 表的欄位

1:setting設定

AUTH_USER_MODEL="app01.UserInfo"     #表示哪張表繼承了auth_user表(在給auth_user新增欄位用到)

2:增加表字段

from django.contrib.auth.models import AbstractUser

class UserInfo(AbstractUser):
    r_pwd
=models.CharField(max_length=32)

3:資料庫遷移

python manage.py makemigrations    #同步
python manage.py migrate

4:效果

auth_user 表名被改為  auth_userinfo。且添加了一個欄位

 5. 資料遷移

makemigrations users 會出錯,django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is
applied before its dependency users.0001_initial on database 'default'.

 

 

 

 

 

 

 



直接:

makemigrations
migrate