zoukankan      html  css  js  c++  java
  • Django 组件content_type

    content type: django内置组件,这个组件帮忙做连表操作(混搭连表)

    适用场景:适用于一张表与多张表同时做关联的时候。直接导入就可以使用了。

    关联数据库所有的表;可以快速插入数据,并且用反向查找就能快速查找到数据。

    models.py文件建立表

    from django.db import models
     
    # Create your models here.
    from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation
    from django.contrib.contenttypes.models import ContentType
     
    class Course(models.Model):
        '''
        普通课程
        '''
        title=models.CharField(max_length=32)
        # 仅用于反向查找
        price_policy_list=GenericRelation('PricePolicy')
     
    class DegreeCourse(models.Model):
        '''
        学位课程
        '''
        title=models.CharField(max_length=32)
        # 仅用于反向查找
        price_policy_list=GenericRelation('PricePolicy')
     
    class PricePolicy(models.Model):
        '''
        价钱策略
        '''
        price = models.IntegerField()
        period = models.IntegerField()
     
        content_type=models.ForeignKey(ContentType,verbose_name='关联的表名称')
        object_id=models.IntegerField(verbose_name='关联的表中的数据行的ID')
     
        content_object=GenericForeignKey('content_type','object_id')
     
    # 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9
    """
    obj = DegreeCourse.objects.filter(title='Python全栈').first()
    # obj.id
    cobj = ContentType.objects.filter(model='course').first()
    # cobj.id
    PricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)
    """

    views.py文件,进行数据插入和查看

    from django.shortcuts import render,HttpResponse
     
    # Create your views here.
    from app01 import models
     
    def test(request):
        # 1 为学位课python添加一个价格策略:一个月9.9
        # obj1=models.DegreeCourse.objects.filter(title='python').first()
        # models.PricePolicy.objects.create(price=9.9,period=30,content_object=obj1)
        #
        # obj1 = models.DegreeCourse.objects.filter(title='python').first()
        # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
        #
        # obj1 = models.DegreeCourse.objects.filter(title='python').first()
        # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)
     
        # # 2 为学位课rest framework添加一个价格策略:一个月9.9
        # obj1=models.Course.objects.filter(title='rest framework').first()
        # models.PricePolicy.objects.create(price=9.9,period=30,content_object=obj1)
        #
        # obj2 = models.Course.objects.filter(title='rest framework').first()
        # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
        #
        # obj3 = models.Course.objects.filter(title='rest framework').first()
        # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)
     
        # 3 根据课程的ID获取课程,并获取该课程的所有价格策略
        course=models.Course.objects.filter(id=1).first()
        price_policys=course.price_policy_list.all()
        print(price_policys)
        return HttpResponse('ok')

     总结:

      当一张表作为多个表的FK,并且只能选择其中一个或者几个时,就可以使用content_type表;例如上面的优惠券表,被食物和衣服当作FK,数据库表一旦建立就难以更改,如果以后需要增加电器等表并把优惠券表作为FK表,这时就不能做到在优惠券表增加列字段electr_id,因为表只能增加行记录而不能增加列字段,因此就可以使用content_type表来将表与表中的对象进行关联,从而做到不增加列字段的情况下增加FK关系。

      在使用content_type表时,需要在FK表中增加content_type作为FK字段,并增加GenericForeignKey便于优惠券表记录的建立以及单个优惠券对象对应的其他商品的查找。在优惠券表关联的“一”的关系表中增加GenericRelation字段便于查找关联的优惠券记录的查找

  • 相关阅读:
    如何创建一个基于 .NET Core 3 的 WPF 项目
    阅读源码系列
    近期计划
    Redis 源码走读(二)对象系统
    Redis 源码走读(一)事件驱动机制与命令处理
    basic paxos解析
    ThreadLocal深度解析
    MySQL InnoDB MVCC深度分析
    java泛型详解
    内存可见性,指令重排序,JIT。。。。。。从一个知乎问题谈起
  • 原文地址:https://www.cnblogs.com/zbw582922417/p/10132879.html
Copyright © 2011-2022 走看看