Django中contenttype的应用

content_type表将app名称与其中的表的关系进行保存

 通过下边的示例来理解content_type的具体应用:

models:

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation
# Create your models here.
class Food(models.Model):
    name = models.CharField(max_length=32)
    coupon = GenericRelation("Coupon")
class Cloth(models.Model):
    name = models.CharField(max_length=32)
    coupon = GenericRelation("Coupon")
class Coupon(models.Model):
    """
    id      food_id     cloth_id  ……
    1         null        null
    2           1         null
    """
    name = models.CharField("活动名称",max_length=64)
    brief = models.TextField(blank=True,null=True,verbose_name="优惠券介绍")
    content_type = models.ForeignKey(ContentType,blank=True,null=True) # 代表哪个app下的哪张表
    object_id = models.PositiveIntegerField("绑定商品",blank=True,null=True) # 代表哪张表中的对象id
    content_obj = GenericForeignKey("content_type","object_id") #不会生成额外的列

view:

from django.shortcuts import render,HttpResponse
from . import models

def test(request):

    # c = models.Cloth.objects.get(id=1)
    # models.Coupon.objects.create(name='优惠券',brief='100减10',content_obj=c) 

    # c = models.Cloth.objects.get(id=1)
    # models.Coupon.objects.create(name='优惠券',brief='200减30',content_obj=c)

    # 查看优惠券绑定的所有课程
    # c = models.Coupon.objects.get(id=1)
    # print(c.content_obj)

    # 查看该商品下的所有优惠券
    c = models.Cloth.objects.get(id=1)
    print(c.coupon.all())
    c2 = models.Cloth.objects.values('name','coupon__brief')
    print(c2)

    return HttpResponse('ok')

 总结:

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

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

原文地址:https://www.cnblogs.com/c491873412/p/7892585.html