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 the M-to-N relationship in django models

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to analyze the M-to-N relationship in django models? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

The following describes the M-to-N relationship in the three relationship models of django models:

M-to-N model

In the SQL language, the MRV N relationship is accomplished by establishing an intermediate relational table that defines foreign keys to the two primary tables. So it is possible to use two 1-type-1-type-1-type-n to define Mv1-n.

At the same time, the django model defines a more direct way to model the MRV N relationship, that is, to define fields of type models,ManyToManyField in either of the two models.

Many-to-many relationship:

1. Define the ManyToManyField field directly

Class Author (models.Model):

Name = models.CharField (max_length=100)

Class Book (models.Model):

Title = models.CharField (max_length=100)

Authors = models.ManyToManyField (Author)

Book= Book.objects.get (title= "Django")

Authors = Book.author_set.all () # get the list of authors of the book

Books= author [2] .book _ set.all () # get all the books written by the third author

2. Define through the intermediate table

Class Book (models.Model):

Title = models.CharField (max_length=100)

Authors = models.ManyToManyField (Author, through= "Authoring")

Class Authoring (models.Model):

Collaboration_type = models.CharField (max_length=100)

Book = models.ForeignKey (Book)

Author = models.ForeignKey (Author)

Chun_essay_compilations = Book.objects.filter (

Author__name__endswith='Chun'

Authoring__collaboration_type='essay'

)

Access many-to-many values (Many-to-Many Values)

Many-to-many works the same way as foreign keys, except that we are dealing with QuerySet rather than model instances. For example, here is how to view the author of a book:

> b = Book.objects.get (id=50) > b.authors.all () [,] > > b.authors.filter (first_name='Adrian') [] > b.authors.filter (first_name='Adam') []

Reverse query is also possible. To view all of an author's books, use author.book_set, like this:

> a = Author.objects.get (first_name='Adrian', last_name='Holovaty') > a.book_set.all () [

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