本文实例讲述了django框架自定义用户表操作。分享给大家供大家参考,具体如下:
django中已经给我生成默认的User表,其中的字段已经可以满足我们的日常需求。
但有时候,我们需要更多的字段,我们就可以自定义。来替换django自带的User表。
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from django.contrib.auth.models import AbstractUser class UserProfile(AbstractUser): nick_name = models.CharField(max_length = 50 , verbose_name = u '昵称' ) birthday = 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 = 11 , verbose_name = u '地址' , null = True , blank = True ) image = models.ImageField(upload_to = 'image/%Y/%m' , default = u "image/default.png" , max_length = 100 ) phone = models.CharField(max_length = 11 , verbose_name = u "手机号码" , null = True , blank = True ) # image依赖Pillow class Meta: verbose_name = u "用户信息" verbose_name_plural = verbose_name def __unicode__( self ): return self .username |
adminx.py
1
2
3
|
from xadmin.plugins.auth import UserAdmin class UserProfileAdmin(UserAdmin): pass |
settings.py
1
2
3
4
5
6
|
INSTALLED_APPS = [ ... 'users' , .... ] AUTH_USER_MODEL = "users.UserProfile" |
然后就可以makemigrations,migrate
然后就可以创建超级用户。如果之前创建过,会被删除,需要重新创建。
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/wongbingming/p/7125713.html