Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze a pair of N Relations in django models

Shulou Source: shulou.com Published: 2022-06-01 16:16:00 09月21日 Update

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.

Tags: Object content time model title classification analysis fields that is skills order knowledge updates differences concise concise one-to-one that is code represents Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi MySQL Shulou Information MariaDB