Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ContentType components in Django

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the ContentType component in Django. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

problem

How to associate multiple tables with foreign keys on one table

From django.db import modelsclass Appliance (models.Model): "" Household Appliances id name 1 refrigerator 2 TV 3 washing Machine "" name = models.CharField (max_length=64) class Food (models.Model): "" Food Table id name 1 bread 2 milk "" name = models.CharField (max_length=32) class Fruit (models.Model) ): "" Fruit Table id name 1 Apple 2 Banana "" name = models.CharField (max_length=32) class Coupon (models.Model): "" coupon list id name appliance_id food_id fruit_id 1 General discount coupon null 2 refrigerator discount coupon 1 null Null 3 TV discount coupon 2 null null 4 Apple full reduction 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) Note

1. Each additional table requires an additional field

Define

When a table is to be associated with multiple tables with foreign keys, we can use the ContentType component provided by Django

ContentTypes is a built-in component of Django that can track the correspondence between all app and model in the project and record it in the ContentType table

App1/models.py

#! / usr/bin/env python3#-*-coding: utf-8-*-from django.db import modelsfrom django.contrib.contenttypes.models import ContentTypefrom django.contrib.contenttypes.fields import GenericForeignKey, GenericRelationclass Food (models.Model): "" id title 1 bread 2 milk "" title = models.CharField (max_length=32) # does not generate coupons field Only used for reverse query coupons = GenericRelation (to= "Coupon") class Fruit (models.Model): "" id title 1 apple 2 banana "" title = models.CharField (max_length=32) class Coupon (models.Model): title = models.CharField (max_length=32) # first step: define the ForeignKey field in model and associate it to the ContentType table content_type = models.ForeignKey (to=ContentType On_delete=None) # step 2: define the IntegerField field, which is used to store the primary key object_id = models.IntegerField () # in the associated table. The third step does not generate the name of the field passed in the above two fields content_object = GenericForeignKey ("content_type", "object_id")

App1\ view.py

Class DemoView (APIView): def get (self, request): # 1. Find the table model content = ContentType.objects.filter (app_label= "app1") through the ContentType table Model= "food") .first () # get table model object equivalent to models.app1 model_class = content.model_class () ret = model_class.objects.all () print (ret) # create a coupon for bread food_obj = Food.objects.filter (id=1). First () Coupon.objects.create (title= "bread 5% discount", content_type_id=8 Object_id=1) Coupon.objects.create (title= "double Eleven package 10% discount Promotion" Content_object=food_obj) # forward query: query the discount object coupon_obj = Coupon.objects.filter (id=1). First () content_obj = coupon_obj.content_object print (content_obj.title) # reverse query: query which coupons are available on bread coupons = food_obj.coupons.all () print (coupons [ 0] .title) # if the reverse query content = ContentType.objects.filter (app_label= "app1") is not defined Model= "food"). First () result = Coupon.objects.filter (content_type=content, object_id=1). All () print (result [0] .name) return Response ("ContentType Test") on "how to use ContentType components in Django" is here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report