您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python之路,Day15 - Django适当进阶篇

5b51 2022/1/14 8:24:07 python 字数 35615 阅读 634 来源 www.jb51.cc/python

本节内容 学员管理系统练习 Django ORM操作进阶 用户认证 Django练习小项目:学员管理系统设计开发 带着项目需求学习是最有趣和效率最高的,今天就来基于下面的需求来继续学习Django 项

概述

本节内容

学员管理系统练习

Django ORM操作进阶

用户认证

Django练习小项目:学员管理系统设计开发

带着项目需求学习是最有趣和效率最高的,今天就来基于下面的需求来继续学习Django 

项目需求:

1.分讲师\学员\课程顾问角色,2.学员可以属于多个班级,学员成绩按课程分别统计3.每个班级至少包含一个或多个讲师4.一个学员要有状态转化的过程,比如未报名前,报名后,毕业老学员5.客户要有咨询纪录,后续的定期跟踪纪录也要保存6.每个学员的所有上课出勤情况\学习成绩都要保存7.学校可以有分校区,认每个校区的员工只能查看和管理自己校区的学员8.客户咨询要区分来源

<span style="color: #0000ff;">from django.db <span style="color: #0000ff;">import<span style="color: #000000;"> models
<span style="color: #0000ff;">from django.contrib.auth.models <span style="color: #0000ff;">import<span style="color: #000000;"> User

class_type_choices= ((<span style="color: #800000;">'<span style="color: #800000;">online<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">网络班<span style="color: #800000;">'<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">offline_weekend<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">面授班(周末)<span style="color: #800000;">'<span style="color: #000000;">,),(<span style="color: #800000;">'<span style="color: #800000;">offline_fulltime<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">面授班(脱产)<span style="color: #800000;">'<span style="color: #000000;">,)
<span style="color: #0000ff;">class<span style="color: #000000;"> UserProfile(models.Model):
user =<span style="color: #000000;"> models.OneToOneField(User)
name = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">姓名<span style="color: #800000;">",max_length=32<span style="color: #000000;">)
<span style="color: #0000ff;">def <span style="color: #800080;">unicode<span style="color: #000000;">(self):
<span style="color: #0000ff;">return<span style="color: #000000;"> self.name

<span style="color: #0000ff;">class<span style="color: #000000;"> School(models.Model):
name = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">校区名称<span style="color: #800000;">",max_length=64,unique=<span style="color: #000000;">True)
addr = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">地址<span style="color: #800000;">",max_length=128<span style="color: #000000;">)
staffs = models.ManyToManyField(<span style="color: #800000;">'<span style="color: #800000;">UserProfile<span style="color: #800000;">',blank=<span style="color: #000000;">True)
<span style="color: #0000ff;">def <span style="color: #800080;">unicode<span style="color: #000000;">(self):
<span style="color: #0000ff;">return<span style="color: #000000;"> self.name

<span style="color: #0000ff;">class<span style="color: #000000;"> Course(models.Model):
name = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">课程名称<span style="color: #800000;">",max_length=128,unique=<span style="color: #000000;">True)
price = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">面授价格<span style="color: #800000;">"<span style="color: #000000;">)
online_price = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">网络班价格<span style="color: #800000;">"<span style="color: #000000;">)
brief = models.TextField(u<span style="color: #800000;">"<span style="color: #800000;">课程简介<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">def <span style="color: #800080;">unicode<span style="color: #000000;">(self):
<span style="color: #0000ff;">return<span style="color: #000000;"> self.name

<span style="color: #0000ff;">class<span style="color: #000000;"> ClassList(models.Model):
course = models.ForeignKey(<span style="color: #800000;">'<span style="color: #800000;">Course<span style="color: #800000;">'<span style="color: #000000;">)
course_type = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">课程类型<span style="color: #800000;">",choices=class_type_choices,max_length=32<span style="color: #000000;">)
semester = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">学期<span style="color: #800000;">"<span style="color: #000000;">)
start_date = models.DateField(u<span style="color: #800000;">"<span style="color: #800000;">开班日期<span style="color: #800000;">"<span style="color: #000000;">)
graduate_date = models.DateField(u<span style="color: #800000;">"<span style="color: #800000;">结业日期<span style="color: #800000;">",blank=True,null=<span style="color: #000000;">True)
teachers = models.ManyToManyField(UserProfile,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">讲师<span style="color: #800000;">"<span style="color: #000000;">)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;def __unicode__(self):</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    return "<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>(<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>)" %(self.course.name,self.course_type)</span>

<span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; <a href="https://www.jb51.cc/tag/Meta/" target="_blank" class="keywords">Meta</a>:
    verbose_name </span>= u<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;班级列表</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
    verbose_name_plural </span>= u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;班级列表</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;
    unique_together </span>= (<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;course</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;course_type</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;semester</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)

<span style="color: #0000ff;">class<span style="color: #000000;"> Customer(models.Model):
qq = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">QQ号<span style="color: #800000;">",unique=<span style="color: #000000;">True)
name = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">姓名<span style="color: #800000;">",max_length=32,null=<span style="color: #000000;">True)
phone = models.BigIntegerField(u<span style="color: #800000;">'<span style="color: #800000;">手机号<span style="color: #800000;">',null=<span style="color: #000000;">True)
stu_id = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">学号<span style="color: #800000;">",null=True,max_length=64<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;">id = models.CharField(u"身份证号",max_length=128)
source_type = ((<span style="color: #800000;">'<span style="color: #800000;">qq<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">qq群<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">referral<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">内部转介绍<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">51cto<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">51cto<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">agent<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">招生代理<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">others<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">其它<span style="color: #800000;">"<span style="color: #000000;">),)
source = models.CharField(u<span style="color: #800000;">'<span style="color: #800000;">客户来源<span style="color: #800000;">',choices=source_type,default=<span style="color: #800000;">'<span style="color: #800000;">qq<span style="color: #800000;">'<span style="color: #000000;">)
referral_from = models.ForeignKey(<span style="color: #800000;">'<span style="color: #800000;">self<span style="color: #800000;">',verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">转介绍自学员<span style="color: #800000;">",help_text=u<span style="color: #800000;">"<span style="color: #800000;">若此客户是转介绍自内部学员,请在此处选择内部学员姓名<span style="color: #800000;">",related_name=<span style="color: #800000;">"<span style="color: #800000;">internal_referral<span style="color: #800000;">"<span style="color: #000000;">)

course </span>= models.ForeignKey(Course,verbose_name=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;咨询课程</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
class_type </span>= models.CharField(u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;班级类型</span><span style="color: #800000;"&gt;"</span>,choices=<span style="color: #000000;"&gt;class_type_choices)
customer_note </span>= models.TextField(u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;客户咨询<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>详情</span><span style="color: #800000;"&gt;"</span>,help_text=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;客户咨询的大概情况,客户个人信息备注等...</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
status_choices </span>= ((<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;signed</span><span style="color: #800000;"&gt;'</span>,u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;已报名</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;),(</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;unregistered</span><span style="color: #800000;"&gt;'</span>,u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;未报名</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;),(</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;graduated</span><span style="color: #800000;"&gt;'</span>,u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;已毕业</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;),)

status </span>= models.CharField(u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;状态</span><span style="color: #800000;"&gt;"</span>,choices=status_choices,default=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;unregistered</span><span style="color: #800000;"&gt;"</span>,help_text=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;选择客户此时的状态</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
consultant </span>= models.ForeignKey(UserProfile,verbose_name=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;课程顾问</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
date </span>= models.DateField(u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;咨询日期</span><span style="color: #800000;"&gt;"</span>,auto_<a href="https://www.jb51.cc/tag/Now/" target="_blank" class="keywords">Now</a>_add=<span style="color: #000000;"&gt;True)

class_list </span>= models.ManyToManyField(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;ClassList</span><span style="color: #800000;"&gt;'</span>,verbose_name=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;已报班级</span><span style="color: #800000;"&gt;"</span>,blank=<span style="color: #000000;"&gt;True)

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__unicode__</span><span style="color: #000000;"&gt;(self):
    </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>,<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a></span><span style="color: #800000;"&gt;"</span> %<span style="color: #000000;"&gt;(self.qq,self.name )

<span style="color: #0000ff;">class<span style="color: #000000;"> ConsultRecord(models.Model):
customer = models.ForeignKey(Customer,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">所咨询客户<span style="color: #800000;">"<span style="color: #000000;">)
note = models.TextField(u<span style="color: #800000;">"<span style="color: #800000;">跟进内容...<span style="color: #800000;">"<span style="color: #000000;">)
status_choices = ((1,u<span style="color: #800000;">"<span style="color: #800000;">近期无报名计划<span style="color: #800000;">"<span style="color: #000000;">),(2,u<span style="color: #800000;">"<span style="color: #800000;">2个月内报名<span style="color: #800000;">"<span style="color: #000000;">),(3,u<span style="color: #800000;">"<span style="color: #800000;">1个月内报名<span style="color: #800000;">"<span style="color: #000000;">),(4,u<span style="color: #800000;">"<span style="color: #800000;">2周内报名<span style="color: #800000;">"<span style="color: #000000;">),(5,u<span style="color: #800000;">"<span style="color: #800000;">1周内报名<span style="color: #800000;">"<span style="color: #000000;">),(6,u<span style="color: #800000;">"<span style="color: #800000;">2天内报名<span style="color: #800000;">"<span style="color: #000000;">),(7,)
status = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">状态<span style="color: #800000;">",help_text=u<span style="color: #800000;">"<span style="color: #800000;">选择客户此时的状态<span style="color: #800000;">"<span style="color: #000000;">)

consultant </span>= models.ForeignKey(UserProfile,verbose_name=u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;跟踪人</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
date </span>= models.DateField(u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;跟进日期</span><span style="color: #800000;"&gt;"</span>,auto_<a href="https://www.jb51.cc/tag/Now/" target="_blank" class="keywords">Now</a>_add=<span style="color: #000000;"&gt;True)

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__unicode__</span><span style="color: #000000;"&gt;(self):
    </span><span style="color: #0000ff;"&gt;return</span> u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>,<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a></span><span style="color: #800000;"&gt;"</span> %<span style="color: #000000;"&gt;(self.customer,self.status)

</span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; <a href="https://www.jb51.cc/tag/Meta/" target="_blank" class="keywords">Meta</a>:
    verbose_name </span>= u<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;客户咨询跟进记录</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
    verbose_name_plural </span>= u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;客户咨询跟进记录</span><span style="color: #800000;"&gt;"</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> CourseRecord(models.Model):
course = models.ForeignKey(ClassList,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">班级(课程)<span style="color: #800000;">"<span style="color: #000000;">)
day_num = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">节次<span style="color: #800000;">",helptext=u<span style="color: #800000;">"<span style="color: #800000;">此处填写第几节课或第几天课程...,必须为数字<span style="color: #800000;">"<span style="color: #000000;">)
date = models.DateField(auto
Now_add=True,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">上课日期<span style="color: #800000;">"<span style="color: #000000;">)
teacher = models.ForeignKey(UserProfile,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">讲师<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">def <span style="color: #800080;">unicode<span style="color: #000000;">(self):
<span style="color: #0000ff;">return u<span style="color: #800000;">"<span style="color: #800000;">%s%s天<span style="color: #800000;">" %<span style="color: #000000;">(self.course,self.day_num)
<span style="color: #0000ff;">class<span style="color: #000000;"> Meta:
verbose_name = u<span style="color: #800000;">'<span style="color: #800000;">上课纪录<span style="color: #800000;">'<span style="color: #000000;">
verbose_name_plural = u<span style="color: #800000;">"<span style="color: #800000;">上课纪录<span style="color: #800000;">"<span style="color: #000000;">
unique_together = (<span style="color: #800000;">'<span style="color: #800000;">course<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">day_num<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> StudyRecord(models.Model):
course_record = models.ForeignKey(CourseRecord,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">第几天课程<span style="color: #800000;">"<span style="color: #000000;">)
student = models.ForeignKey(Customer,verbose_name=u<span style="color: #800000;">"<span style="color: #800000;">学员<span style="color: #800000;">"<span style="color: #000000;">)
record_choices = ((<span style="color: #800000;">'<span style="color: #800000;">checked<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">已签到<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">late<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">迟到<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">noshow<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">缺勤<span style="color: #800000;">"<span style="color: #000000;">),(<span style="color: #800000;">'<span style="color: #800000;">leave_early<span style="color: #800000;">',u<span style="color: #800000;">"<span style="color: #800000;">早退<span style="color: #800000;">"<span style="color: #000000;">),)
record = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">上课纪录<span style="color: #800000;">",choices=record_choices,default=<span style="color: #800000;">"<span style="color: #800000;">checked<span style="color: #800000;">",max_length=64<span style="color: #000000;">)
score_choices = ((100,<span style="color: #800000;">'<span style="color: #800000;">A+<span style="color: #800000;">'<span style="color: #000000;">),(90,<span style="color: #800000;">'<span style="color: #800000;">A<span style="color: #800000;">'<span style="color: #000000;">),(85,<span style="color: #800000;">'<span style="color: #800000;">B+<span style="color: #800000;">'<span style="color: #000000;">),(80,<span style="color: #800000;">'<span style="color: #800000;">B<span style="color: #800000;">'<span style="color: #000000;">),(70,<span style="color: #800000;">'<span style="color: #800000;">B-<span style="color: #800000;">'<span style="color: #000000;">),(60,<span style="color: #800000;">'<span style="color: #800000;">C+<span style="color: #800000;">'<span style="color: #000000;">),(50,<span style="color: #800000;">'<span style="color: #800000;">C<span style="color: #800000;">'<span style="color: #000000;">),(40,<span style="color: #800000;">'<span style="color: #800000;">C-<span style="color: #800000;">'<span style="color: #000000;">),(0,<span style="color: #800000;">'<span style="color: #800000;">D<span style="color: #800000;">'<span style="color: #000000;">),(-1,<span style="color: #800000;">'<span style="color: #800000;">N/A<span style="color: #800000;">'<span style="color: #000000;">),(-100,<span style="color: #800000;">'<span style="color: #800000;">COPY<span style="color: #800000;">'<span style="color: #000000;">),(-1000,<span style="color: #800000;">'<span style="color: #800000;">FAIL<span style="color: #800000;">'<span style="color: #000000;">),)
score = models.IntegerField(u<span style="color: #800000;">"<span style="color: #800000;">本节成绩<span style="color: #800000;">",choices=scorechoices,default=-1<span style="color: #000000;">)
date = models.DateTimeField(auto
Now_add=<span style="color: #000000;">True)
note = models.CharField(u<span style="color: #800000;">"<span style="color: #800000;">备注<span style="color: #800000;">",max_length=255,null=<span style="color: #000000;">True)

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__unicode__</span><span style="color: #000000;"&gt;(self):
    </span><span style="color: #0000ff;"&gt;return</span> u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>,学员:<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>,纪录:<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>,成绩:<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a></span><span style="color: #800000;"&gt;"</span> %<span style="color: #000000;"&gt;(self.course_record,self.student.name,self.record,self.get_<a href="https://www.jb51.cc/tag/score/" target="_blank" class="keywords">score</a>_display())

</span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; <a href="https://www.jb51.cc/tag/Meta/" target="_blank" class="keywords">Meta</a>:
    verbose_name </span>= u<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;学员学习纪录</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
    verbose_name_plural </span>= u<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;学员学习纪录</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;
    unique_together </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;course_record</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;student</span><span style="color: #800000;"&gt;'</span>)</pre>

常用ORM操作


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶