zoukankan      html  css  js  c++  java
  • django——contentType组件

    contentType组件

    django内置的一个组件,方便我们快速的进行连表操作,查询,插入数据

    使用方法:

    在course表中:
                policy = GenericRelation('PricePolicy', object_id_field='course_id', content_type_field='table_id')

    在价格策略表中:
            content_obj = GenericForeignKey('table_id','course_id')
            --加的这两个字段都不会在数据库中生成,它只是用来查询,插入

    models—数据库

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
    
    
    # 专题课表
    class Course(models.Model):
        title = models.CharField(max_length=32)
        # 不需要做数据库迁移,不会在数据表中生成,是用来方便查询
        policy=GenericRelation('PricePolice',object_id_field='course_id',content_type_field='table_id')
    
        def __str__(self):
            return self.title
    
    
    # 学位课表
    class DegreeClass(models.Model):
        title = models.CharField(max_length=32)
        policy = GenericRelation('PricePolice', object_id_field='course_id', content_type_field='table_id')
    
        def __str__(self):
            return self.title
    
    
    # 轻课表
    class LightCourse(models.Model):
        title = models.CharField(max_length=32)
        policy = GenericRelation('PricePolice', object_id_field='course_id', content_type_field='table_id')
    
    
        def __str__(self):
            return self.title
    
    
    # 价格策略表
    class PricePolice(models.Model):
        price = models.DecimalField(max_digits=8, decimal_places=2)
        # 价格周期
        period = models.CharField(max_length=32)
        # 情调:如果是外部导入的表,不能带引号,
        # 表的id
        table_id = models.ForeignKey(to=ContentType)
        # 课程id
        course_id = models.IntegerField()
        # 如果表id:字段名叫:content_type,课程id字段名叫object_id GenericForeignKey就不需要传参数
        # 也不需要做数据库迁移,也不会在数据库生成字段,这个只用来做查询和插入
        # 如果保存的时候,只需要传content_obj这个字段,内部会自动传入table_id,course_id
        content_obj = GenericForeignKey('table_id', 'course_id')
        # PositiveIntegerField----正整数

    view——视图层

    from django.shortcuts import render, HttpResponse
    from django.contrib.contenttypes.models import ContentType
    from app01 import models

    # def test(request):
    # 为django专题课,创建三个价格策略
    # course = models.Course.objects.get(pk=1)
    # 老方法:很麻烦
    # table_id = ContentType.objects.get(model='course')
    # ret = models.PricePolice.objects.create(price=9.9, period='1个月', table_id=table_id, course_id=course.pk)
    # ret = models.PricePolice.objects.create(price=19.9, period='2个月', table_id=table_id, course_id=course.pk)
    # ret = models.PricePolice.objects.create(price=29.9, period='3个月', table_id=table_id, course_id=course.pk)

    # 为django专题课,创建三个价格策略
    # 新方法:使用ContentType插入
    # course=models.Course.objects.get(pk=2)
    # ret=models.PricePolice.objects.create(price='13.3',period='1个月',content_obj=course)
    # ret=models.PricePolice.objects.create(price='23.3',period='2个月',content_obj=course)
    # ret=models.PricePolice.objects.create(price='33.3',period='3个月',content_obj=course)

    # 给学位课加一个价格策略
    # 新方法:使用ContentType插入
    # degree_course = models.DegreeClass.objects.get(pk=1)
    # ret = models.PricePolice.objects.create(price='188888', period='5个月', content_obj=degree_course)

    # 删除
    # course=models.PricePolice.objects.get(course_id=1).delete()

    # 查询所有的价格策略,并且显示对应的课程名称
    # ret = models.PricePolice.objects.all()
    # for i in ret:
    # print(type(i.content_obj))
    # print(i.content_obj)

    # 通过课程id,获取课程信息和价格策略
    # 查询django所有的价格策略
    # course=models.Course.objects.get(pk=2)
    # course_police=course.policy.all()
    # for i in course_police:
    # print(type(i))
    # print(i.period)

    # return HttpResponse('ok')
    -contenttype组件:
            -帮助我们快速连表,可以跟多个表做关联
            -Course表:
                -id
                -title
                -price_policy=GenericRelation(to='PricePolicy', object_id_field='course_id', content_type_field='table_id')
                    -object_id_field课程id
                    -content_type_field表id
                    -不会再数据库创建字段,只用来查询
            可以快速的通过price_policy字段快速的取出该课程的所有价格策略
    
            -PricePolicy
                -id
                -price
                -period
                -course_id:课程id,IntegerField
                -table_id:表id
                -content_obj = GenericForeignKey('table_id','course_id')
                    -content_obj通过这个字段,可以快速的查询出该价格策略的课程
                    -存的时候,这个字段,直接放一个对象
                    -这个字段,不会再数据库生成,只用来查询,插入
  • 相关阅读:
    算法思想杂谈【原创】
    OpenGL坐标变换专题
    XSS的原理分析与解剖:第三章(技巧篇)【转】
    php实现字符串翻转
    (基础) --- php session原理和多台服务器session共享问题
    (基础) --- php get和post的区别
    (基础)--- PHP单引号和双引号区别
    MySQL主从复制原理解析
    详解MYSQL各种优化原理
    mysql索引详解
  • 原文地址:https://www.cnblogs.com/cao123/p/10153157.html
Copyright © 2011-2022 走看看