反向查询
反向查询
路飞学城项目
一、建模型
models
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()
模型表进行注册:
admin
from django.contrib import admin # Register your models here. from app01 import models from .models import __all__ for table in __all__: models =getattr(models.table) admin.site.register(models)
settings里设置 redis ,并且安装redis软件.
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' CACHES={ "default":{ "BACKEND":"django_redis.cache.RedisCache", "LOCATION":"redis://127.0.0.1:6379", "OPTION":{ "CLIENT_CLASS":"django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS":{"max_connections":1000} #"PASSWORD":"密码" } }, "back01":{ 'BACKEND':"django_redis.cache.RedisCache", "LOCATION":"redis://127.0.0.1:6379", "OPTION":{ "CLIENT_CLASS":"django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS":{"max_connections":1000} #"PASSWORD":"密码", } } }
序列化组件
from rest_framework import serializers from app01.models import Course,Chapter,CourseDetail,OftenAskedQuestion class CourseSerializer(serializers.ModelSerializer): class Meta: model =Course fields ="__all__" class CourseDetailSerializer(serializers.ModelSerializer): class Meta: model =CourseDetail fields ="__all__" course =serializers.CharField(source ="course.title") price_policy =serializers.SerializerMethodField() def get_price_policy(self,obj): #课程详情对应的课程相关的所有价格策略 price_policy =obj.course.price_policy.all() return [{"id":item.pk,"price":item.price ,"valid_period":item.get_valid_period_display()} for item in price_policy] recommend_courses=serializers.SerializerMethodField() def get_recommend_course(self,obj): tem=[] for course in obj.recommend_course.all(): tem.append({"id":course.id,"title":course.title}) return tem class ChapterSerializer(serializers.ModelSerializer): class Meta: model =Chapter fields ="__all__" class QuestionsSerializer(serializers.ModelSerializer): class Meta: model =OftenAskedQuestion fields ="__all__"
Auth 认证
from rest_framework.authentication import BaseAuthentication from app01.models import Usertoken from rest_framework.exceptions import AuthenticationFailed class LuffyLoginAuth(BaseAuthentication): def authenticate(self, request): token =request.query_params.get("token") token_obj=Usertoken.objects.filter(token=token).first() if token_obj: return token_obj.user.user,token_obj else: raise AuthenticationFailed("认证失败")
全局 认证:
局部认证.
查看页面
爬虫技术
添加购物车
1 防止 恶意攻击 ,没有意义的课程号或者价格
执行如下代码
输出的结果:
执行 自定义的课程与价格代码