django重写用户模型

重写一个UserProfile继承自带的AbstractUser

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime

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


# Create your models here.


class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default='')
    birthday = models.DateField(verbose_name=u"生日", null=True, blank=True)
    gender = models.CharField(max_length=5, verbose_name=u"性别", choices=(("male", u"男"), ("female", u"女")), default="female")
    address = models.CharField(max_length=100, default=u"")
    mobile = models.CharField(max_length=11, null=True,blank=True)
    image = models.ImageField(upload_to="image/%Y/%m/%d", default=u"image/default.png", max_length=100)

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

    def __unicode__(self):
        return self.username

在settings内增加定义

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"

其中AUTH_USER_MODEL = "users.UserProfile"一定要在第一次迁移数据库前做,要不会出现错误,出现错误要删掉以前数据库
具体参考https://www.jianshu.com/p/b993f4feff83

posted on 2018-12-25 23:41  神秘藏宝室  阅读(504)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<