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 analyze a pair of N Relations in django models

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to analyze a pair of N relationships in django models. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The following describes a pair of N relationships in the three relationship models of django models:

A pair of N models

In Django language, the 1 N relationship is accomplished by setting a foreign key reference to the 'main table' in the 'schedule'. At the SQL model layer, foreign keys can be defined with fields of type models,Foreignkey.

''

Content classification table

''

Class concat (models.Model):

# Category id

Id = models.BigAutoField (primary_key=True)

# when the parent category ID=0, it represents the first level category

Parent_id = models.BigIntegerField (default=0)

# first name

Name = models.CharField (max_length=50)

# status (optional values: 1 (normal), 2 (delete))

Status = models.IntegerField (default=1)

# arrange the sequence number to indicate the order in which the purposes of the sibling class are displayed, or in the order of name if the values are equal. Value range: integers greater than zero

Sort_order = models.IntegerField (default=1)

# whether this category is the parent category, 1 is true,0 and false

Is_parent = models.SmallIntegerField (default=1)

# creation time

Created = models.DateTimeField (auto_now_add=True)

# Update time

Updated = models.DateTimeField (auto_now=True)

''

Content table

''

Class content (models.Model):

# content id

Id = models.AutoField (primary_key=True)

# Category id

Cid = models.ForeignKey (concat, on_delete=models.CASCADE)

# title

Title = models.CharField (max_length=1000)

# subtitle

Sub_title = models.CharField (max_length=1000)

# title description

Title_desc = models.CharField (max_length=500)

# Link

Url = models.CharField (max_length=500)

# the absolute path of the picture

Pic = models.CharField (max_length=300)

# content

Content = models.TextField (default= "")

# creation time

Created = models.DateTimeField (auto_now_add=True)

# Update time

Updated = models.DateTimeField (auto_now=True)

The only difference between the above code and the one-to-one relationship is that the cid field in the content model is defined with models.ForeignKey. So that each concat object can be associated with multiple content objects

Queinfo = que_info.objects.all ()

For quein queinfo:

QueCat = que.cid.name

# get the master object by attaching it

Print que.cid

Print queCat

His master object can be obtained directly by clicking 'cid', that is, a foreign key.

A = que_cat.objects.all ()

For a1 in a:

A1.que_info_set

# getting the attached object through the main object is how to analyze the pair of N relations in django models. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report