最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django ContentType組件詳解

 更新時間:2021年12月06日 14:53:45   作者:小Pawn爺  
這篇文章主要為大家介紹了Django ContentType組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

問題

如何在一張表上對多個表進行外鍵關(guān)聯(lián)

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):
    """
    優(yōu)惠券表
    id  name    appliance_id    food_id     fruit_id
    1   通用優(yōu)惠券   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)

注意

1.每增加一張表就需要多增加一個字段,

定義

當一張表要跟多張表進行外鍵關(guān)聯(lián)的時候,我們可以使用Django提供的ContentType 組件

ContentTypes是Django內(nèi)置的一個組件,可以追蹤項目中所有app和model的對應關(guān)系,并記錄在ContentType表中

app1/models.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

class Food(models.Model):
    """
    id      title
    1       面包
    2       牛奶
    """
    title = models.CharField(max_length=32)
    # 不會生成coupons字段,只用于反向查詢
    coupons = GenericRelation(to="Coupon")

class Fruit(models.Model):
    """
    id      title
    1       蘋果
    2       香蕉
    """
    title = models.CharField(max_length=32)

class Coupon(models.Model):
    title = models.CharField(max_length=32)
    # 第一步:在 model中定義ForeignKey字段,并關(guān)聯(lián)到ContentType表
    content_type = models.ForeignKey(to=ContentType, on_delete=None)
    # 第二步:定義IntegerField字段,用來存儲關(guān)聯(lián)表中的主鍵
    object_id = models.IntegerField()
    # 第三步 不會生成字段傳入上面兩個字段的名字
    content_object = GenericForeignKey("content_type", "object_id")

app1\view.py

class DemoView(APIView):
    def get(self, request):
        # 1.通過ContentType表找表模型
        content = ContentType.objects.filter(app_label="app1", model="food").first()
        # 獲得表model對象 相當于models.app1
        model_class = content.model_class()
        ret = model_class.objects.all()
        print(ret)
        # 給面包創(chuàng)建一個優(yōu)惠券
        food_obj = Food.objects.filter(id=1).first()
        Coupon.objects.create(title="面包九五折", content_type_id=8, object_id=1)
        Coupon.objects.create(title="雙十一面包九折促銷", content_object=food_obj)
        # 正向查詢:根據(jù)優(yōu)惠信息查詢優(yōu)惠對象
        coupon_obj = Coupon.objects.filter(id=1).first()
        content_obj = coupon_obj.content_object
        print(content_obj.title)
        # 反向查詢:查詢面包都有哪些優(yōu)惠券
        coupons = food_obj.coupons.all()
        print(coupons[0].title)
        # 如果沒定義反向查詢
        content = ContentType.objects.filter(app_label="app1", model="food").first()
        result = Coupon.objects.filter(content_type=content, object_id=1).all()
        print(result[0].name)
        return Response("ContentType測試")

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論

三亚市| 湘潭县| 葵青区| 东乌珠穆沁旗| 盈江县| 乐业县| 巴东县| 北宁市| 中山市| 灵宝市| 蓬溪县| 东宁县| 新干县| 灌云县| 广宁县| 兰考县| 逊克县| 龙井市| 宁化县| 绍兴县| 霸州市| 黔西县| 桂阳县| 定远县| 海林市| 昭苏县| 叙永县| 东乌| 三穗县| 灌南县| 社会| 永定县| 辰溪县| 安岳县| 古浪县| 泉州市| 淮安市| 军事| 滁州市| 淮北市| 陆河县|