Django ContentType組件

ContentType組件

引入

如今咱們有這樣一個需求~咱們的商城裏有不少的商品~~節日要來了~咱們要搞活動~~django

那麼咱們就要設計優惠券~~優惠券都有什麼類型呢~~滿減的~折扣的~立減的~~app

咱們對應着咱們活動類型~對咱們的某類商品設計優惠券~~好比~~ui

家電是一類商品~~食物是一類商品~那麼咱們能夠設計家電折扣優惠券~~以及食物滿減優惠券等~spa

那麼咱們看錶結構怎麼設計設計

from django.db import models class Appliance(models.Model): """ 家用電器表 id name 1 冰箱 2 電視 3 洗衣機 """ name = models.CharField(max_length=64) 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)<br># 實際上咱們商品的種類會特別的多,致使咱們這張表外鍵愈來愈多

 

遇到這種一張表要跟多張表進行外鍵關聯的時候~咱們Django提供了ContentType組件~rest

ContentType組件

ContentType是Django的內置的一個應用,能夠追蹤項目中全部的APP和model的對應關係,並記錄在ContentType表中。code

當咱們的項目作數據遷移後,會有不少django自帶的表,其中就有django_content_type表,咱們能夠去看下對象

 

ContentType組件應用:blog

  -- 在model中定義ForeignKey字段,並關聯到ContentType表,一般這個字段命名爲content-typeget

  -- 在model中定義PositiveIntergerField字段, 用來存儲關聯表中的主鍵,一般咱們用object_id

  -- 在model中定義GenericForeignKey字段,傳入上面兩個字段的名字

  --  方便反向查詢能夠定義GenericRelation字段

代碼以下:

from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import 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)
    # 第一步
    content_type = models.ForeignKey(to=ContentType) # 第二步
    object_id = models.PositiveIntegerField() # 第三步
    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)
相關文章
相關標籤/搜索