zoukankan      html  css  js  c++  java
  • Django-ConttentType

    一 content-type

    在django中,有一个记录了项目中所有model元数据的表,就是ContentType,表中一条记录对应着一个存在的model,所以可以通过一个ContentType表的id和一个具体表中的id找到任何记录,及先通过ContenType表的id可以得到某个model,再通过model的id得到具体的对象。

    二 使用方式

    表结构:

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    
    from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
    
    
    class DegreeCourse(models.Model):
        """学位课程"""
        name = models.CharField(max_length=128, unique=True)
        course_img = models.CharField(max_length=255, verbose_name="缩略图")
        brief = models.TextField(verbose_name="学位课程简介", )
    
    
    class Course(models.Model):
        """专题课程"""
        name = models.CharField(max_length=128, unique=True)
        course_img = models.CharField(max_length=255)
    
        # 不会在数据库生成列,只用于帮助你进行查询
        policy_list = GenericRelation("PricePolicy")
    
    
    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()

    使用:

    # 1.在价格策略表中添加一条数据
    # models.PricePolicy.objects.create(
    #     valid_period=7,
    #     price=6.6,
    #     content_type=ContentType.objects.get(model='course'),
    #     object_id=1
    # )
    
    # models.PricePolicy.objects.create(
    #     valid_period=14,
    #     price=9.9,
    #     content_object=models.Course.objects.get(id=1)
    # )
    
    # 2. 根据某个价格策略对象,找到他对应的表和数据,如:管理课程名称
    # price = models.PricePolicy.objects.get(id=2)
    # print(price.content_object.name) # 自动帮你找到
    
    # 3.找到某个课程关联的所有价格策略
    # obj = models.Course.objects.get(id=1)
    # for item in obj.policy_list.all():
    #     print(item.id,item.valid_period,item.price)
    #

    # 4.通过ContentType表找模型
    content = ContentType.objects.filter(app_label='demo', model='degree_course')
    model_class = content.model_class()
      ret = model_class.objects.all()

    三 使用总结

    当一张表中需要与多张表建立ForeignKey并且只能填写其中一个或者几个时,就可以使用content_type表。例如上例中价格策略表可能会对应不同类型的课程, 可以通过content_type字段来描述该条数据属于哪张表,通过object_id来记录外键ID

    增加GenericRelation可以方便于反向查询但是不会生成数据库列

     
  • 相关阅读:
    js模板引擎
    浮点数正则表达式
    DbContext SQLite配置文件
    JS中的HTML片段
    WPF 使用HttpListener搭建本地web服务器
    C#调用Windows(8/10)自带的虚拟键盘
    SQLSERVER 设置默认值
    SQLSERVER存储过程基本语法
    MSSQL存储过程
    WPF手动绑定事件
  • 原文地址:https://www.cnblogs.com/harryblog/p/10469189.html
Copyright © 2011-2022 走看看