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自带的表,其中就有django_content_type表app

ContentType组件应用:ide

  -- 在model中定义ForeignKey字段,并关联到ContentType表,一般这个字段命名为content-type测试

  -- 在model中定义PositiveIntergerField字段, 用来存储关联表中的主键,一般咱们用object_idui

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

  --  方便反向查询能够定义GenericRelation字段rest

基于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,就能找到优惠券对应的商品code

 

当一张表跟多张表有外键关系时,均可以经过 ContentType 组件来创建对象

 

ContentType 查询blog

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

相关文章
相关标签/搜索