这次的模型继承AbstractUser类,里面自带了一些属性,再新建下边的属性:
from django.db import models
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
nickname = models.CharField(max_length=50,verbose_name="昵称",default='')
birday = models.DateField(verbose_name = "生日",null=True,blank=True)
gender = models.CharField(choices=(("male","男"), ("female","女")), max_length=5, default="female")
address = models.CharField(max_length=100,default="")
mobile = models.CharField(max_length=11,null=True,blank=True)
image = models.ImageField(upload_to="image/%Y/%m",default="image/default.png")
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.username
其中ImageField功能需要pillow包,安装到虚拟环境里。settings里把users放到installed_apps里,再加上个
AUTH_USER_MODEL = "users.UserProfile"
其中users为app名,UserProfile为模型类名
然后数据迁移makemigration和migrate。
本项目需要3个主要应用,包括users、organisations、courses。要避免这三个应用循环引用,可以再创建一个应用叫operations,用于处理这三个应用的交互。另外有些功能用的比较少,随便找个地方放就行,我们就决定放在users里了。包括验证邮箱发验证码和轮播图。都放在users的models.py里了。
from django.db import models
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
nickname = models.CharField(max_length=50,verbose_name="昵称",default='')
birday = models.DateField(verbose_name = "生日",null=True,blank=True)
gender = models.CharField(choices=(("male","男"), ("female","女")), max_length=5, default="female")
address = models.CharField(max_length=100,default="")
mobile = models.CharField(max_length=11,null=True,blank=True)
image = models.ImageField(upload_to="image/%Y/%m",default="image/default.png")
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.username
class EmailVerifyRecord(models.Model):
code = models.CharField(max_length=20, verbose_name="验证码")
email = models.EmailField(max_length=50,verbose_name="邮箱")
send_type = models.CharField(choices=(("register","注册"),("forget","忘记密码")),max_length=10)
send_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = "邮箱验证码"
verbose_name_plural = verbose_name
class Banner(models.Model):
title = models.CharField(max_length=100, verbose_name="标题")
image= models.ImageField(upload_to="banner/%Y/%m", verbose_name="轮播图",max_length=100)
url = models.URLField(max_length=200,verbose_name="访问地址")
index = models.IntegerField(default=100, verbose_name="顺序")
add_time = models.DateTimeField(default=datetime.now,verbose_name="添加时间")
class Meta:
verbose_name = "轮播图"
verbose_name_plural = verbose_name
其中验证码类里的send_time用于记录发验证码的时间,已设置超时过期。default=datetime.now中如果写now()则会返回创建项目的时间。datetime功能需要import。轮播图的url用于记录每个图片的超链接地址,index用于记录轮播图的序号(顺序)。