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字段便于查找关联的优惠券记录的查找