zoukankan      html  css  js  c++  java
  • qhfl-2 ContentType组件

    一般商城里有很多的商品,计优惠券对应着活动类型商品,家电是一类商品,食物是一类商品,优惠券对应着不同的商品类别。
    
    from django.db import models
    
    class Appliance(models.Model):
        """
        家用电器表
        id name
      冰箱
      电视
      洗衣机
        """
        name = models.CharField(max_length=64)
    
    
    class Food(models.Model):
        """
        食物表
        id name
     面包
     牛奶
        """
        name = models.CharField(max_length=32)
    
    
    class Fruit(models.Model):
        """
        水果表
        id  name
      苹果
      香蕉
        """
        name = models.CharField(max_length=32)
    
    
    class Coupon(models.Model):
        """
        优惠券表
        id  name    appliance_id    food_id     fruit_id
      通用优惠券   null            null        null
      冰箱折扣券   1               null        null
      电视折扣券   2               null        null
      苹果满减卷   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)<br># 实际上我们商品的种类会特别的多,导致我们这张表外键越来越多
    遇到这种一张表要跟多张表进行外键关联的时候~我们Django提供了ContentType组件~
    
    需求
    需求

    ContentType组件

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

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

    ContentType组件应用:

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

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

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

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

    基于ContentType创建表结构

    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):
        """
        id      title
        1       面包
        2       牛奶
        """
        title = models.CharField(max_length=32)
        # 不会生成字段 只用于反向查询
        coupons = GenericRelation(to="Coupon")
    
    
    class Fruit(models.Model):
        """
        id      title
        1       苹果
        2       香蕉
        """
        title = models.CharField(max_length=32)
    
    
    # 如果有40张表
    # class Coupon(models.Model):
    #     """
    #     id      title          food_id    fruit_id
    #     1       面包九五折         1         null
    #     2       香蕉满10元减5元    null       2
    #     """
    #     title = models.CharField(max_length=32)
    #     food = models.ForeignKey(to="Food")
    #     fruit = models.ForeignKey(to="Fruit")
    
    
    # class Coupon(models.Model):
    #     """
    #     id      title        table_id      object_id
    #     1       面包九五折       1             1
    #     2       香蕉满10元减5元  2             2
    #     """
    #     title = models.CharField(max_length=32)
    #     table = models.ForeignKey(to="Table")
    #     object_id = models.IntegerField()
    #
    #
    # class Table(models.Model):
    #     """
    #     id      app_name       table_name
    #     1       demo            food
    #     2       demo            fruit
    #     """
    #     app_name = models.CharField(max_length=32)
    #     table_name = models.CharField(max_length=32)
    view
    class Coupon(models.Model):
        title = models.CharField(max_length=32) # 优惠券的名称
        # 第一步
        content_type = models.ForeignKey(to=ContentType, on_delete=None)  # 商品表
        # 第二步
        object_id = models.IntegerField() # 商品对象的id
        # 第三步 不会生成字段
        content_object = GenericForeignKey("content_type", "object_id")
        #                            # 绑定外键关系,防止商品id不在商品表里面

    这样无论有多少张优惠券,都只需要外键关联Content_type一张表(里面存着所有表的关系),然后指明对象的id,就能找到优惠券对应的商品

    当一张表跟多张表有外键关系时,都可以通过 ContentType 组件来建立

    ContentType 查询

    demo/views.py

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from .models import Food, Coupon
    from django.contrib.contenttypes.models import ContentType
    
    class DemoView(APIView):
    
        def get(self, request):
    
            # 给面包创建一个优惠券
            food_obj = Food.objects.filter(id=1).first()
            # Coupon.objects.create(title="面包九五折", content_type_id=8, object_id=1)  # 比较麻烦,需要找表id
            # Coupon.objects.create(title="双十一面包九折促销", content_object=food_obj)
    
            # 查询面包都有哪些优惠券
            coupons = food_obj.coupons.all()
            print(coupons)
            # 优惠券查对象
            coupon_obj = Coupon.objects.filter(id=1).first()
            content_obj = coupon_obj.content_object
            print(coupon_obj.title)
    
    """
    <QuerySet [<Coupon: Coupon object (1)>, <Coupon: Coupon object (2)>, <Coupon: Coupon object (3)>, <Coupon: Coupon object (4)>]>
    面包九五折
    """
    
            # 通过ContentType表找表模型
            content = ContentType.objects.filter(app_label="demo", model="food").first()
            print(content)
            model_class = content.model_class()
            ret = model_class.objects.all()
            print(ret)
    """food
    <QuerySet [<Food: Food object (1)>, <Food: Food object (2)>]>
            return Response("ContentType测试")
    """

    image

  • 相关阅读:
    @Transactional 什么情况下会失效?
    如何主持一场专业的面试?
    MIT-HIB 心率数据库及相关
    hadoop中Writable类
    XXX.jar has no source attachment
    Win10Eclipse配置个人本地hadoop
    js去除两个数组中重复的元素
    JS找出两个数组中不相同的元素
    flex中order控制元素的排列顺序
    flex中align-self设置侧轴的某元素的对其方式
  • 原文地址:https://www.cnblogs.com/wenyule/p/10466483.html
Copyright © 2011-2022 走看看