Django ContentType組件詳解
問題
如何在一張表上對多個表進行外鍵關聯(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.每增加一張表就需要多增加一個字段,
定義
當一張表要跟多張表進行外鍵關聯(lián)的時候,我們可以使用Django提供的ContentType 組件
ContentTypes是Django內(nèi)置的一個組件,可以追蹤項目中所有app和model的對應關系,并記錄在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字段,并關聯(lián)到ContentType表 content_type = models.ForeignKey(to=ContentType, on_delete=None) # 第二步:定義IntegerField字段,用來存儲關聯(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測試")
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
解決django xadmin主題不顯示和只顯示bootstrap2的問題
這篇文章主要介紹了解決django xadmin主題不顯示和只顯示bootstrap2的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python 實現(xiàn)docx與doc文件的互相轉換
這篇文章主要介紹了python 實現(xiàn)docx與doc文件的互相轉換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03python3+PyQt5實現(xiàn)自定義流體混合窗口部件
這篇文章主要為大家詳細介紹了python3+PyQt5實現(xiàn)自定義流體混合窗口部件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04python中for循環(huán)輸出列表索引與對應的值方法
今天小編就為大家分享一篇python中for循環(huán)輸出列表索引與對應的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11