先附上整个系统的表结构设计:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Customer(models.Model):
"""客户信息表"""
name = models.CharField(max_length=32,blank=True,null=True)#blank的作用是,admin操作时,允许null
qq = models.CharField(max_length=64,unique=True)
qq_name = models.CharField(max_length=32,blank=True,null=True)
phone = models.CharField(max_length=64,blank=True,null=True)
source_choices = ((0,'转介绍'),
(1, 'QQ群'),
(2, '官网'),
(3, '百度推广'),
(4, '51CTO'),
(5, '知乎'),
(6, '市场推广'),
)
source = models.SmallIntegerField(choices=source_choices)
referral_form = models.CharField(verbose_name="转介绍人qq",max_length=64,null=True,blank=True)
consult_course = models.ForeignKey("Course",verbose_name="咨询课程",on_delete=models.CASCADE)
content = models.TextField(verbose_name="咨询详情")
tags = models.ManyToManyField("Tag",blank=True,null=True)
consultant = models.ForeignKey("UserProfile",on_delete=models.CASCADE)#课程顾问,外键账户表
memo = models.TextField(blank=True,null=True)#备注
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.qq
class Tag(models.Model):
name = models.CharField(unique=True,max_length=32)
def __str__(self):
return self.name
class CustomerFollowUp(models.Model):
"""客户跟进表"""
customer = models.ForeignKey("Customer",on_delete=models.CASCADE)
content = models.TextField(verbose_name="跟进内容")
consultant = models.ForeignKey("UserProfile", on_delete=models.CASCADE)#课程顾问,外键账户表
date = models.DateTimeField(auto_now_add=True)
intention_choices = ((0,'2周内报名'),
(1,'1个月内报名'),
(2,'近期无报名计划'),
(3,'已在其它机构报名'),
(4,'已报名'),
(5,'已拉黑'),
)
intention = models.SmallIntegerField(choices=intention_choices)
def __str__(self):
return "<%s : %s>" %(self.customer.qq, self.intention)
class Course(models.Model):
"""课程表"""
name = models.CharField(max_length=64,unique=True)
price = models.PositiveSmallIntegerField()#学费,positive表示正数
period = models.PositiveSmallIntegerField(verbose_name="周期(月)")#周期
outline = models.TextField()#大纲
def __str__(self):
return self.name
class Branch(models.Model):
"""校区表"""
name = models.CharField(max_length=128,unique=True)
addr = models.CharField(max_length=128)
def __str__(self):
return self.name
class ClassList(models.Model):
"""班级表"""
branch = models.ForeignKey("Branch",on_delete=models.CASCADE,verbose_name="校区")
course = models.ForeignKey("Course",on_delete=models.CASCADE)
class_type_choices = ((0,'面授(脱产)'),
(1,'面授(周末)'),
(2,'网络班'),
)
class_type = models.PositiveSmallIntegerField(choices=class_type_choices,verbose_name="班级类型")
semester = models.PositiveSmallIntegerField(verbose_name="学期")
teachers = models.ManyToManyField("UserProfile")
start_date = models.DateField(verbose_name="开班日期")
end_date = models.DateField(verbose_name="结业日期",blank=True,null=True)
def __str__(self):
return "%s %s %s" %(self.branch,self.course,self.semester)
class Meta:
unique_together = ('branch','course','semester')
class CourseRecord(models.Model):
"""上课记录"""
from_class = models.ForeignKey("ClassList",verbose_name="班级",on_delete=models.CASCADE)
day_num = models.PositiveSmallIntegerField(verbose_name="第几节(天)")
teacher = models.ForeignKey("UserProfile",on_delete=models.CASCADE)
has_homework = models.BooleanField(default=True)
homework_title = models.CharField(max_length=128,blank=True,null=True)
homework_content = models.TextField(blank=True,null=True)
outline = models.TextField(verbose_name="本节课大纲")
date = models.DateField(auto_now_add=True)
def __str__(self):
return "%s %s " %(self.from_class,self.day_num)
class Meta:
unique_together = ("from_class","day_num")
class StudyRecord(models.Model):
"""学习记录表"""
student = models.ForeignKey("Enrollment",on_delete=models.CASCADE)
course_record = models.ForeignKey("CourseRecord",on_delete=models.CASCADE)
attendance_choices = ((0,'已签到'),
(1,'迟到'),
(2,'缺勤'),
(3,'早退'),
)
attendance = models.SmallIntegerField(choices=attendance_choices,default=0)
score_choices = ((100,"A+"),
(90,"A"),
(85,"B+"),
(80,"B"),
(75,"B-"),
(70,"C+"),
(60,"C"),
(40,"C-"),
(0,"N/A"),#Not Available
(-50,"D"),
(-100,"COPY"),
)
score = models.SmallIntegerField(choices=score_choices)
memo = models.TextField(blank=True,null=True)#备注
date = models.DateField(auto_now_add=True)
def __str__(self):
return "%s %s %s" %(self.student,self.course_record,self.score)
class Enrollment(models.Model):
"""报名表"""
customer = models.ForeignKey("Customer",on_delete=models.CASCADE)
enrolled_class = models.ForeignKey("ClassList",verbose_name="所报班级",on_delete=models.CASCADE)
consultant = models.ForeignKey("UserProfile",verbose_name="课程顾问",on_delete=models.CASCADE)
contract_agreed = models.BooleanField(default=False,verbose_name="学员已同意合同")
contract_approved = models.BooleanField(default=False,verbose_name="合同已审核")
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "%s %s" %(self.customer,self.enrolled_class)
class Meta:
unique_together = ("customer","enrolled_class")
class Payment(models.Model):
"""缴费记录"""
customer = models.ForeignKey("Customer",on_delete=models.CASCADE)
course = models.ForeignKey("Course",on_delete=models.CASCADE)
amount = models.PositiveIntegerField(verbose_name="数额",default=500)#交的钱,定金500
consultant = models.ForeignKey("UserProfile",on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "%s %s" %(self.customer,self.amount)
class UserProfile(models.Model):
"""账号表"""
user = models.OneToOneField(User,on_delete=models.CASCADE)
name = models.CharField(max_length=32)
roles = models.ManyToManyField("Role",null=True,blank=True)
def __str__(self):
return self.name
class Role(models.Model):
"""角色表"""
name = models.CharField(max_length=32,unique=True)
def __str__(self):
return self.name
接下来连接MySQL数据库:
首先可能会遇到pip需要更新的提示,利用如下代码解决:
python -m pip install --upgrade pip
pip install 包名-i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
1.python2中Django连接数据库使用的是MySQLdb这个模块
2.python3.5+中Django连接数据库使用的是pymsql这个模块,并且不再支持MySQLdb
于是,我们需要下载pymysql这个模块
第一步:
pip install pymysql
第二步,在_init_.py文件中添加代码:
import pymysql
pymysql.install_as_MySQLdb()
这个时候执行python manage.py makemigrations 可能会报如下错误:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
第一步:
# 找到base.py文件,注释掉 base.py 中如下部分(35/36行)
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
第二步:
#找到operations.py文件(146行),将decode改为encode
#linux vim 查找快捷键:?decode
if query is not None:
query = query.decode(errors='replace')
return query
#改为
if query is not None:
query = query.encode(errors='replace')
return query
执行语句,成功连接数据库:
python manage.py makemigrations
python manage.py migrate