一、项目环境
- 语言:python3
- IDE:pycharm
- 组件:bootstarp,jQuery
二、模型
2.1编写模型
联合唯一
class Meta:显示中文名。
认证使用django自带的User表,跟我们的用户表进行关联
from django.contrib.auth.models import User
1 from django.db import models 2 from django.contrib.auth.models import User 3 4 # Create your models here. 5 6 7 class Customer(models.Model): 8 """客户信息表""" 9 name = models.CharField(max_length=32, blank=True, null=True) 10 #32个字节,中文10个,第一次来不会告诉你为空,blank是在django-admin里面的限制。一般都是成对出现 11 qq = models.CharField(max_length=64,unique=True) 12 #以qq为主,可以是手机,微信 13 phone = models.CharField(max_length=64,blank=True,null=True) 14 source_choices = ( 15 ("0","转介绍"), 16 ("1","QQ群"), 17 ("2","官网"), 18 ("3","百度推广"), 19 ("4","51CTO"), 20 ("5","知乎"), 21 ("6","市场推广"), 22 ) 23 source = models.SmallIntegerField(choices=source_choices) 24 referral_name = models.CharField(verbose_name="转介绍人qq",max_length=64,blank=True,null=True) 25 #咨询的课程 26 consult_course = models.ForeignKey("Course", verbose_name="咨询课程") 27 #描述客户信息 28 content = models.TextField(verbose_name="咨询详情") 29 consultant = models.ForeignKey("UserProfile") 30 memo = models.TextField(blank=True,null=True) 31 tags = models.ManyToManyField("Tag",blank=True,null=True) 32 date = models.DateField(auto_now_add=True) 33 34 def __str__(self): 35 return self.qq 36 class Meta: 37 verbose_name_plural = "客户表" 38 39 class Tag(models.Model): 40 name = models.CharField(unique=True,max_length=32) 41 42 def __str__(self): 43 return self.name 44 class Meta: 45 verbose_name_plural = "标签表" 46 47 class CustomerFollowUp(models.Model): 48 """客户跟进表""" 49 customer = models.ForeignKey("Customer") 50 content = models.TextField("跟进内容") 51 consultant = models.ForeignKey("UserProfile") 52 intention_choices = ( 53 (0,"两周内报名"), 54 (1,"1个月内报名"), 55 (2,"近期无报名计划"), 56 (3,"已在其他机构报名"), 57 (4,"已报名"), 58 (5,"已拉黑"), 59 ) 60 intention = models.SmallIntegerField(choices=intention_choices) 61 date = models.DateTimeField(auto_now_add=True) 62 def __str__(self): 63 return "<%s :%s>"%(self.customer,self.intention) 64 class Meta: 65 verbose_name_plural = "客户跟进表" 66 67 68 69 70 71 class Course(models.Model): 72 """课程表""" 73 name = models.CharField(max_length=64,unique=True) 74 price = models.PositiveIntegerField() 75 #周期 76 period = models.PositiveIntegerField(verbose_name="周期(月)") 77 outline = models.TextField() 78 def __str__(self): 79 return self.name 80 class Meta: 81 verbose_name_plural = "课程表" 82 83 84 class ClassList(models.Model): 85 """班级表""" 86 branch = models.ForeignKey("Branch") 87 #创建班级的时候,关联课程 88 course = models.ForeignKey("Course") 89 class_type_choices = ( 90 (0,"面授-脱产"), 91 (0,"面授-周末"), 92 (0,"面授-周末"), 93 ) 94 class_type = models.SmallIntegerField(choices=class_type_choices,verbose_name="授课方式") 95 semester = models.PositiveSmallIntegerField("学期") 96 teachers = models.ManyToManyField("UserProfile") 97 star_date = models.DateField(verbose_name="开班日期") 98 end_date = models.DateField(verbose_name="结业日期",blank=True,null=True) 99 100 def __str__(self): 101 return "%s %s %s"%(self.branch,self.course,self.semester) 102 class Meta: 103 unique_together = ('branch','course','semester') 104 verbose_name_plural = "班级表" 105 106 107 class Branch(models.Model): 108 """校区""" 109 name = models.CharField(max_length=128,unique=True) 110 addr = models.CharField(max_length=128) 111 def __str__(self): 112 return self.name 113 class Meta: 114 verbose_name_plural = "校区" 115 116 class CourseRecord(models.Model): 117 """上课记录""" 118 from_class = models.ForeignKey("ClassList",verbose_name="班级") 119 day_num = models.PositiveSmallIntegerField(verbose_name="第几节") 120 teacher = models.ForeignKey("UserProfile") 121 has_homework = models.BooleanField(default=True) 122 homework_tittle = models.CharField(max_length=128,blank=True,null=True) 123 homework_content = models.TextField(blank=True,null=True) 124 outline = models.TextField(verbose_name="本节课程大纲") 125 date = models.DateField(auto_now_add=True) 126 127 128 def __str__(self): 129 return "%s %s"%(self.from_class,self.day_num) 130 131 class Meta: 132 unique_together = ("from_class","day_num") 133 verbose_name_plural = "上课记录" 134 135 class StudyRecord(models.Model): 136 """学习记录表""" 137 student = models.ForeignKey("Enrollment") 138 course_record = models.ForeignKey("CourseRecord") 139 attendance_choices = ( 140 (0,"已签到"), 141 (1,"迟到"), 142 (2,"缺钱"), 143 (3,"早退"), 144 ) 145 attendance = models.SmallIntegerField(choices=attendance_choices) 146 #使用数字便于统计 147 score_choices = ((100,"A+"), 148 (90,"A"), 149 (85,"B+"), 150 (80,"B"), 151 (75,"B-"), 152 (70,"C+"), 153 (60,"C"), 154 (40,"C-"), 155 (-50,"D"), 156 (-100,"COPY"), 157 (0,"N/A"), 158 ) 159 score = models.SmallIntegerField(choices=score_choices) 160 memo = models.TextField(blank=True,null=True) 161 date = models.DateField(auto_now_add=True) 162 def __str__(self): 163 return "%s %s %s" %(self.student,self.course_record,self.score) 164 class Meta: 165 unique_together = ('student','course_record') 166 verbose_name_plural = "学习记录表" 167 168 169 """ 170 一个学生报完名,需要合同(可以报名多个) 171 上课记录等等,所以需要一个报名表 172 学生本人的信息在 客户信息表 中, 173 """ 174 class Enrollment(models.Model): 175 """报名表""" 176 customer = models.ForeignKey("Customer") 177 enrolled_class = models.ForeignKey("ClassList",verbose_name="所报班级") 178 consultant = models.ForeignKey("UserProfile",verbose_name="课程顾问") 179 contract_agreed = models.BooleanField(default=False,verbose_name="学院已同意合同条款") 180 contract_approved = models.BooleanField(default=False,verbose_name="合同与审核") 181 date = models.DateTimeField(auto_now_add=True) 182 183 def __str__(self): 184 return "%s %s"%(self.customer,self.enrolled_class) 185 class Meta: 186 unique_together = ("customer","enrolled_class") 187 verbose_name_plural = "报名表" 188 189 190 class Payment(models.Model): 191 """缴费记录""" 192 customer = models.ForeignKey("Customer") 193 course = models.ForeignKey("Course",verbose_name="所报课程") 194 amount = models.PositiveIntegerField(verbose_name="金额",default=500)#定金500 195 consultant = models.ForeignKey("UserProfile") 196 date = models.DateTimeField(auto_now_add=True) 197 198 def __str__(self): 199 return "%s %s" %(self.customer,self.amount) 200 class Meta: 201 verbose_name_plural = "缴费记录表" 202 203 204 class UserProfile(models.Model): 205 """账号表""" 206 """跟djaogo的user表进行关联,这样认证等问题,就可以很简单的解决了""" 207 user = models.OneToOneField(User) 208 name = models.CharField(max_length=32) 209 roles = models.ManyToManyField("Role",blank=True,null=True) 210 class Meta: 211 verbose_name_plural = "账号表" 212 213 214 215 class Role(models.Model): 216 """角色表""" 217 name = models.CharField(max_length=32,unique=True) 218 def __str__(self): 219 return self.name 220 class Meta: 221 verbose_name_plural = "角色表"
2.2 注册表
该注册表是为了在admin中可以显示该表。
1 from django.contrib import admin
2 from crm import models
3
4 admin.site.register(models.Customer)
5 admin.site.register(models.CustomerFollowUp)
6 admin.site.register(models.Enrollment)
7 admin.site.register(models.Course)
8 admin.site.register(models.ClassList)
9 admin.site.register(models.CourseRecord)
10 admin.site.register(models.Branch)
11 admin.site.register(models.Role)
12 admin.site.register(models.Payment)
13 admin.site.register(models.StudyRecord)
14 admin.site.register(models.Tag)
15 admin.site.register(models.UserProfile)
2.3 数据库同步
Python manage.py makemigrations
python manage.py migrate
2.4 创建超级管理员
python manage.py createsuperuser
2.5 登入后台
2.6 数据显示
我们在模型中定义了很多字段,但是admin中只显示了id列。so 我们需要自定制admin
class CustomerAdmin(admin.ModelAdmin):
list_display = ('id','qq','source','consultant','content','status','date')
list_filter = ('source','consultant','date')
search_fields = ('qq','name')
raw_id_fields = ('consult_course',)
filter_horizontal = ('tags',)
list_editable = ('status','consultant')#可在信息表中直接修改
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','name')
admin.site.register(models.UserProfile,UserProfileAdmin)
admin.site.register(models.Customer,CustomerAdmin)
效果图如下,非常的明显: