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

    ContentType组件

    遇到这一张表要跟多张表进行外键关联的时候~我们Django提供了ContentType组件~

    ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中。

    当我们的项目做数据迁移后,会有很多django自带的表,其中就有django_content_type表,我们可以去看下~~~

    ContentType组件应用:

      -- 在model中定义ForeignKey字段,并关联到ContentType表,通常这个字段命名为content-type

      -- 在model中定义PositiveIntergerField字段, 用来存储关联表中的主键,通常我们用object_id

      -- 在model中定义GenericForeignKey字段,传入上面两个字段的名字

      --  方便反向查询可以定义GenericRelation字段

    建模:

    class Appliance(models.Model):
        """
        家用电器表
        id name
        1   冰箱
        2   电视
        3   洗衣机
        """
        name = models.CharField(max_length=64)
        coupons = GenericRelation(to="Coupon")  # 自用于反向查询 不生成字段
     
     
    class Food(models.Model):
        """
        食物表
        id name
        1  面包
        2  牛奶
        """
        name = models.CharField(max_length=32)
     
     
    class Fruit(models.Model):
        """
        水果表
        id  name
        1   苹果
        2   香蕉
        """
        name = models.CharField(max_length=32)
     
     
    class Coupon(models.Model):
        """
        优惠券表
        id  name    appliance_id    food_id     fruit_id
        1   通用优惠券   null            null        null
        2   冰箱折扣券   1               null        null
        3   电视折扣券   2               null        null
        4   苹果满减卷   null            null        1
        我每增加一张表就要多增加一个字段
        """
        name = models.CharField(max_length=32)
        # appliance = models.ForeignKey(to="Appliance", null=True, blank=True)
        # food = models.ForeignKey(to="Food", null=True, blank=True)
        # fruit = models.ForeignKey(to="Fruit", null=True, blank=True)
        # 第一步 去ContentType表跟表绑定关系
        content_type = models.ForeignKey(to=ContentType)
        # 第二步 对象的id
        object_id = models.PositiveIntegerField()
        # 第三步 通过外检关系给表以及对象id绑定关系 得到对象
        content_object = GenericForeignKey('content_type', 'object_id')  

    使用:

    from django.http import HttpResponse
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from django.contrib.contenttypes.models import ContentType
    from .models import Appliance, Coupon
     
    # Create your views here.
     
     
    class Test(APIView):
     
        def get(self, request):
            # 通过ContentType获得表名
            content = ContentType.objects.filter(app_label="app01", model="appliance").first()
            # 获得表model对象 相当于models.Applicance
            model_class = content.model_class()
            ret = model_class.objects.all()
     
            # 为海尔冰箱创建一条优惠记录
            ice_box = Appliance.objects.filter(id=1).first()
            Coupon.objects.create(name="海尔冰箱折扣券", content_object=ice_box)
     
            # 查询优惠券id=1绑定了哪个商品
            coupon_obj = Coupon.objects.filter(id=1).first()
            goods_obj = coupon_obj.content_object
            print(goods_obj.name)
     
            # 查询海尔冰箱的所有优惠券 id=1
            # 我们定义了反向查询
            results = ice_box.coupons.all()
            print(results[0].name)
     
            # 如果没定义反向查询
            content = ContentType.objects.filter(app_label="app01", model="appliance").first()
            result = Coupon.objects.filter(content_type=content, object_id=1).all()
            print(result[0].name)
            return HttpResponse(ret)
  • 相关阅读:
    PHP ftp_rmdir() 函数
    PHP ftp_rename() 函数
    PHP ftp_rawlist() 函数
    PHP ftp_raw() 函数
    PHP ftp_pwd() 函数
    PHP ftp_put() 函数
    PHP ftp_pasv() 函数
    PHP ftp_nlist() 函数
    PHP ftp_nb_get() 函数
    PHP ftp_nb_fget() 函数
  • 原文地址:https://www.cnblogs.com/yidashi110/p/10126872.html
Copyright © 2011-2022 走看看