一、表结构设计
1 from django.db import models
2 from django.contrib.auth.models import User
3
4 """自带验证"""
5 class UserProFile(models.Model):
6 """用户信息表"""
7 user = models.OneToOneField(User,on_delete=models.CASCADE)
8 name = models.CharField(max_length=32,verbose_name='姓名')
9 role = models.ManyToManyField('Role',null=True,blank=True)
10
11 class Meta:
12 verbose_name_plural = '用户信息表'
13
14 def __str__(self): # __unicode__
15 return self.name
16
17 class Role(models.Model):
18 """角色表"""
19 name = models.CharField(max_length=64,unique=True)
20 menus = models.ManyToManyField('Menus', verbose_name='菜单',blank=True)
21
22 class Meta:
23 verbose_name_plural = '角色表'
24
25 def __str__(self):
26 return self.name
27
28 class CustomerInfo(models.Model):
29 """客户信息"""
30 name = models.CharField(max_length=32,default=None)
31 contact_type_choices = (
32 (0,'qq'),
33 (1,'微信'),
34 (2,'手机'),
35 )
36 contact_type = models.SmallIntegerField(choices=contact_type_choices,default=0)
37 contact = models.CharField(max_length=64,unique=True)
38 source_choices = (
39 (0,'QQ群'),
40 (1,'51CTO'),
41 (2,'百度推广'),
42 (3,'知乎'),
43 (4,'转介绍'),
44 (5,'其他'),
45 )
46 source = models.SmallIntegerField(choices=source_choices)
47 referral_from = models.ForeignKey('self',null=True,blank=True,verbose_name='转介绍',on_delete=models.CASCADE)
48
49 consult_courses = models.ManyToManyField('Course',verbose_name='咨询课程')
50 consult_content = models.TextField(verbose_name='咨询内容')
51 status_choices = (
52 (0,'未报名'),
53 (1,'已报名'),
54 (2,'已退学'),
55 )
56 status = models.SmallIntegerField(choices=status_choices)
57 consultant = models.ForeignKey('UserProFile',verbose_name='课程顾问',on_delete=models.CASCADE)
58 date = models.DateField(auto_now_add=True)
59
60 class Meta:
61 verbose_name_plural = '客户信息表'
62
63 def __str__(self):
64 return self.name
65
66 class Student(models.Model):
67 """学员表"""
68 customer = models.ForeignKey('CustomerInfo',on_delete=models.CASCADE)
69 class_grade = models.ManyToManyField('ClassList')
70
71 class Meta:
72 verbose_name_plural = '学员表'
73
74 def __str__(self):
75 return self.customer
76
77 class CustomerFollowUp(models.Model):
78 """客户跟踪记录表"""
79 customer = models.ForeignKey('CustomerInfo',on_delete=models.CASCADE)
80 content = models.TextField(verbose_name='跟踪内容')
81 user = models.ForeignKey('UserProFile',verbose_name='跟进人',on_delete=models.CASCADE)
82 status_choices = (
83 (0,'近期无报名计划'),
84 (1,'一个月内报名'),
85 (2,'2周内报名'),
86 (3,'已报名'),
87 )
88 status = models.SmallIntegerField(choices=status_choices)
89 date = models.DateField(auto_now_add=True)
90
91 class Meta:
92 verbose_name_plural = '客户跟踪记录表'
93
94 def __str__(self):
95 return self.content
96
97 class Course(models.Model):
98 """课程表"""
99 name = models.CharField(verbose_name='课程名称',max_length=64,unique=True)
100 price = models.PositiveSmallIntegerField() # 必须为正
101 period = models.PositiveSmallIntegerField(verbose_name='课程周期(月)',default=5)
102 outline = models.TextField(verbose_name='大纲')
103
104 class Meta:
105 verbose_name_plural = '课程表'
106
107 def __str__(self):
108 return self.name
109
110 class ClassList(models.Model):
111 """班级列表"""
112 branch = models.ForeignKey('Branch',on_delete=models.CASCADE)
113 course = models.ForeignKey('Course',on_delete=models.CASCADE)
114 class_type_choices = (
115 (0,'脱产'),
116 (1,'周末'),
117 (2,'网络班')
118 )
119 class_type = models.SmallIntegerField(choices=class_type_choices)
120 semester = models.SmallIntegerField(verbose_name='学期')
121 teachers = models.ManyToManyField('UserProFile',verbose_name='讲师')
122 start_date = models.DateField('开班日期')
123 graduate_date = models.DateField('毕业日期',blank=True,null=True)
124
125 class Meta:
126 verbose_name_plural = '班级列表'
127 unique_together = (
128 'course',
129 'semester',
130 'branch',
131 'class_type',
132 )
133
134 def __str__(self):
135 return '%s(%s)期' %(self.course.name, self.semester)
136
137 class CourseRecord(models.Model):
138 """上课记录"""
139 class_grade = models.ForeignKey('ClassList',verbose_name='上课班级',on_delete=models.CASCADE)
140 day_num = models.PositiveSmallIntegerField('课程节次')
141 teacher = models.ForeignKey('UserProFile',on_delete=models.CASCADE)
142 title = models.CharField('本节主题',max_length=64)
143 content = models.TextField('本节内容')
144 has_homework = models.BooleanField('本节有作业',default=True)
145 homework = models.TextField('作业需求',blank=True,null=True)
146 date = models.DateTimeField(auto_now_add=True)
147
148 class Meta:
149 verbose_name_plural = '上课记录'
150 unique_together = (
151 'class_grade',
152 'day_num',
153 )
154
155 def __str__(self):
156 return '%s第(%s)节' %(self.class_grade, self.day_num)
157
158 class StudyRecord(models.Model):
159 """学习记录"""
160 course_record = models.ForeignKey('CourseRecord',on_delete=models.CASCADE)
161 student = models.ForeignKey('Student',on_delete=models.CASCADE)
162
163 score_choices = (
164 (100,'A+'),
165 (90,'A'),
166 (85,'B+'),
167 (80,'B'),
168 (75,'B-'),
169 (70,'C+'),
170 (60,'C'),
171 (40,'C-'),
172 (-50,'D'),
173 (0,'N/A'), # not avaliable
174 (-100,'COPY'),
175 )
176 score = models.SmallIntegerField(choices=score_choices,default=0)
177 show_choices = (
178 (0,'缺勤'),
179 (1,'已签到'),
180 (2,'迟到'),
181 (3,'早退'),
182 )
183 show_status = models.SmallIntegerField(choices=show_choices)
184 note = models.TextField('成绩备注',blank=True,null=True)
185 date = models.DateTimeField(auto_now_add=True)
186
187 class Meta:
188 verbose_name_plural = '学习记录'
189
190 def __str__(self):
191 return '%s %s %s' %(self.course_record,self.student,self.score)
192
193 class Branch(models.Model):
194 """校区"""
195 name = models.CharField(max_length=64,unique=True)
196 addr = models.CharField(max_length=128,blank=True,null=True)
197
198 class Meta:
199 verbose_name_plural = '校区'
200
201 def __str__(self):
202 return self.name
203
204 class Menus(models.Model):
205 """动态菜单"""
206 name = models.CharField('菜单名',max_length=32)
207 url_type_choices = (
208 (0,'absolute'),
209 (1,'dynamic'),
210 )
211 url_type = models.SmallIntegerField(choices=url_type_choices,default=0)
212 url_name = models.CharField('连接',max_length=128)
213
214
215 class Meta:
216 verbose_name_plural = '菜单'
217 unique_together = ('name','url_name')
218
219 def __str__(self):
220 return self.name
总结:
1、防止以后掉坑,需多花心思设计表结构
2、先整体把所有表列出,一定给表写注释
3、在逐一完善字段,并确定个表之间的关系
4、后续添加
5、注意细节,变量命名的规范,单复数
6、日后若想用Django自带验证时,用户需关联Django的User(O2O)