扩展auth_user表
一、关于表的介绍
1、创建一个
django
项目使用命令行生成的表mysql> show tables; +----------------------------+ | Tables_in_django_rest01 | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 10 rows in set (0.00 sec)
2、关于用户表的结构
mysql> desc auth_user; +--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | password | varchar(128) | NO | | NULL | | | last_login | datetime(6) | YES | | NULL | | | is_superuser | tinyint(1) | NO | | NULL | | | username | varchar(150) | NO | UNI | NULL | | | first_name | varchar(30) | NO | | NULL | | | last_name | varchar(150) | NO | | NULL | | | email | varchar(254) | NO | | NULL | | | is_staff | tinyint(1) | NO | | NULL | | | is_active | tinyint(1) | NO | | NULL | | | date_joined | datetime(6) | NO | | NULL | | +--------------+--------------+------+-----+---------+----------------+ 11 rows in set (0.01 sec) mysql>
二、自己去扩展django
自带的用户表
1、导包
from django.contrib.auth.models import AbstractUser
2、定义自己的模型来继承
AbstractUser
class UserProfileModel(AbstractUser): """ 创建一个用户的模型 """ nick_name = models.CharField(max_length=50, verbose_name='昵称', null=False, default='') birthday = models.DateField(verbose_name='生日', null=True, blank=True) gender = models.CharField(max_length=20, choices=(('male', '男'), ('female', '女')), default='male', verbose_name='性别') address = models.CharField(max_length=100, default='', verbose_name='地址') mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机号码') image = models.ImageField(upload_to='images/users/%Y/%m', default='images/default.jepg', verbose_name='头像') create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间') update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间') class Meta(object): verbose_name = '用户信息' verbose_name_plural = verbose_name def __str__(self): return self.username
3、查看表结构(相对比上面的表结构多了自己定义的字段)
mysql> desc users_userprofilemodel; +--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | password | varchar(128) | NO | | NULL | | | last_login | datetime(6) | YES | | NULL | | | is_superuser | tinyint(1) | NO | | NULL | | | username | varchar(150) | NO | UNI | NULL | | | first_name | varchar(30) | NO | | NULL | | | last_name | varchar(150) | NO | | NULL | | | email | varchar(254) | NO | | NULL | | | is_staff | tinyint(1) | NO | | NULL | | | is_active | tinyint(1) | NO | | NULL | | | date_joined | datetime(6) | NO | | NULL | | | nick_name | varchar(50) | NO | | NULL | | | birthday | date | YES | | NULL | | | gender | varchar(20) | NO | | NULL | | | address | varchar(100) | NO | | NULL | | | mobile | varchar(11) | YES | | NULL | | | image | varchar(100) | NO | | NULL | | | create_at | datetime(6) | NO | | NULL | | | update_at | datetime(6) | NO | | NULL | | +--------------+--------------+------+-----+---------+----------------+ 19 rows in set (0.01 sec) mysql>
三、需要在配置文件中设置使用我们自己定义的数据表
1、配置如下
# 配置认证的用户表 AUTH_USER_MODEL = 'users.UserProfileModel'