0%

Extend-Django-Fields

扩展 Django 用户字段

源码来自:《Python Django Web 典型模块开发实战》

整理:CK

1
2
3
4
5
6
7
8
9
10
11
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config', # <----
'rest_framework'
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# app01/models.py

from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
"""
用户
"""
is_auther=models.BooleanField(default=False,verbose_name='是否认证')
phone=models.CharField(max_length=11,verbose_name='电话')
email = models.CharField(max_length=100,null=True,blank=True,verbose_name='邮箱')
add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
class Meta:
verbose_name='用户'
verbose_name_plural = verbose_name
def __str__(self):
return self.username

class Key(models.Model):
"""
key表
"""
author=models.ForeignKey(UserProfile,verbose_name='开发者', on_delete=models.CASCADE)
app_name=models.CharField(max_length=10,verbose_name='应用名称')
key=models.CharField(max_length=32,verbose_name='应用key值')
add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
class Meta:
verbose_name='key表'
verbose_name_plural = verbose_name
def __str__(self):
return self.key
1
AUTH_USER_MODEL='app01.UserProfile'
1
2
python manage.py makemigrations
python manage.py migrate