1. 程式人生 > >Django自定義用戶表替換默認用戶表認證

Django自定義用戶表替換默認用戶表認證

django

1、自定義用戶表

from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class UserProfile(AbstractUser):   ##引用Django內置的AbstractUser表,並添加下列字段
    nick_name = models.CharField(max_length=50,default="",verbose_name=u"昵稱")
    birday = models.DateField(verbose_name=u"生日",null=True,blank=True)
    gender = models.CharField(max_length=10,choices=(("male",u"男"),("female",u"女")),default="female")
    address = models.CharField(max_length=100,default=u"",verbose_name=‘地址‘)
    mobile = models.CharField(max_length=11,null=True,blank=True,verbose_name=‘手機號碼‘)
    image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png",max_length=100)

    class Meta:
        verbose_name  = u"用戶信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.username


2、在settings註冊APP,並配置用戶表model參數AUTH_USER_MODEL

INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘users‘
]

AUTH_USER_MODEL = "users.UserProfile"

Django自定義用戶表替換默認用戶表認證