from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation from django.contrib.contenttypes.models import ContentType # Create your models here. from django.db import models __all__ =["User","Usertoken","Course","CourseDetail","Chapter","CourseSection","OftenAskedQuestion","PricePolicy"] ##认证模型 class User(models.Model): user=models.CharField(max_length=32) pwd =models.CharField(max_length=32) class Usertoken(models.Model): user =models.OneToOneField("User") token=models.CharField(max_length=128) #1.课程表 class Course(models.Model): """ 课程表 """ title =models.CharField(verbose_name="课程名称",max_length=32) course_img =models.CharField(verbose_name="课程图片",max_length=64) level_choices=( (1,"初级"), (2,"中级"), (3,"高级"), ) level =models.IntegerField(verbose_name="课程难易程度",choices=level_choices,default=1) #用GenericForeignKey反向查询,不会生成表字段,切勿删除. price_policy =GenericRelation("PricePolicy") asked_question =GenericRelation("OftenAskedQuestion") def __str__(self): return self.title #2.课程详情表 class CourseDetail(models.Model): """ 课程详情 """ course =models.OneToOneField(to="Course") slogon=models.CharField(verbose_name="口号",max_length=255) recommend_courses =models.ManyToManyField(verbose_name="推荐课程",to="Course",related_name="rc",null=True,blank=True) def __str__(self): return "课程详情:"+self.course.title #3.章节表 class Chapter(models.Model): """ 章节 """ num= models.IntegerField(verbose_name="章节") name= models.CharField(verbose_name="章节名称",max_length=32) course =models.ForeignKey(verbose_name="所属课程",to= "Course") def __str__(self): return self.name #4.课时表 class CourseSection(models.Model): """ 课时目录 """ chapter =models.ForeignKey("Chapter",related_name="course_sections") name =models.CharField(max_length=128) class Meta: verbose_name_plural="11.课时" #5.常见问题表.#用到content-type class OftenAskedQuestion(models.Model): """ 常见问题 """ content_type =models.ForeignKey(ContentType, limit_choices_to={"model__contains":"course"}) #关联 course or degree_course object_id =models.PositiveIntegerField() content_object =GenericForeignKey("content_type","object_id") question =models.CharField(max_length=255) answer =models.TextField(max_length=1024) def __str__(self): return "%s-%s"%(self.content_object,self.question) class Meta: unique_together=("content_type","object_id","question") verbose_name_plural="08.常见问题" #6.价格课程表.#用到content-type class PricePolicy(models.Model): """ 价格与课程的有效期表 """ content_type =models.ForeignKey(ContentType) #关联 course or degree_course object_id =models.PositiveIntegerField() content_object =GenericForeignKey("content_type","object_id") valid_period_choices= ((1,"1天"),(3,"3天"), (7,"1周"),(14,"2周"), (30,"1个月"), (60,"2个月"), (90,"3个月"), (180,"6个月"),(210,"12个月"), (540,"18个月"),(720,"24个月"), ) valid_period =models.SmallIntegerField(choices=valid_period_choices) price =models.FloatField() def __str__(self): return str(self.content_object)+ "====>"+str(self.price) #优惠券表 class Coupon(models.Model): "优惠券生成规则 " name =models.CharField(max_length=64,verbose_name="活动名称") brief = models.TextField(blank=True,null=True,verbose_name="优惠券介绍") coupon_type_choice =((0,"立减劵"),(1,"满减券"),(2,"折扣卷")) coupon_type =models.SmallIntegerField(choices=coupon_type_choice,default=0,verbose_name="券类型") """ 通用: money_equivalent_value=300 off_percent =null minimum_consume =0 满减: money_equivalent_value =100 off_percent=null minimum_consume =1000 折扣: money_equivalent_value =0 off_percent =79 minimum_consume =0 """ money_equivalent_value =models.IntegerField(verbose_name="等值货币",blank=True,null=True) off_percent= models.PositiveIntegerField("折扣百分比",help_text="只针对折扣卷,例7.9,写成79",blank=True,null=True) minimum_consume =models.PositiveIntegerField("最低消费",default=0,help_text="仅在满减卷时填写此字段") content_type =models.ForeignKey(ContentType,blank=True,null=True) object_id =models.PositiveIntegerField("绑定课程",blank=True,null=True,help_text="可以把优惠券和课程绑定") content_object =GenericForeignKey("content_type","object_id") quantity =models.PositiveIntegerField("数量(张)",default=1) open_date = models.DateField("优惠券领取开始时间") close_date =models.DateField("优惠券结束时间") valid_begin_date =models.DateField(verbose_name="有效期开始时间",blank= True,null=True) valid_end_date =models.PositiveIntegerField(verbose_name="有效期结束时间",blank=True,null=True) coupon_valid_days= models.PositiveIntegerField(verbose_name="优惠券有效期(天)",blank=True,null=True, help_text="自优惠券被领开始算起") date =models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural="31.优惠券生成记录" def __str__(self): return "%s(%s)"%(self.get_coupon_type_display(),self.name) def save(self,*args,**kwargs): if not self.coupon_valid_days or (self.valid_begin_date and self.valid_end_date): if self.valid_begin_date and self.valid_end_date: raise ValueError("valid_end_date 有效期结束日期必须晚于 valid_begin_date") if self.coupon_valid_days ==0: raise ValueError("coupon_valid_days 有效期不能为0") if self.coupon_valid_days ==0: raise ValueError("coupon_valid_days 有效期不能为0 ") if self.close_date <self.open_date: raise ValueError("close_date 优惠券领取结束时间必须晚于open_date优惠券领取开始时间") super(Coupon, self).save(*args,**kwargs) class CouponRecord(models.Model): """优惠券发放,消费记录""" coupon =models.ForeignKey("Coupon") number =models.CharField(max_length=64,unique=True) user =models.ForeignKey("User",verbose_name="拥有者") status_choices =((0,"未使用"),(1,"已使用"),(2,"已过期")) status= models.SmallIntegerField(choices=status_choices,default=0) get_time =models.DateTimeField(verbose_name="领取时间",help_text="用户领取时间") used_time =models.DateTimeField(blank=True,null=True,verbose_name="使用时间") class Meta: verbose_name_plural ="32.用户优惠券" def __str__(self): return "%s-%s-%s"%(self.user,self.number,self.status)