In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you what is the method of Django ManyToManyField query across intermediate tables, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The method of query across Intermediate tables in Django ManyToManyField
1. Manytomany is used in the django table to generate the intermediate table pyclub_article_column.
From django.db import models
# Create your models here.
Class Column (models.Model):
Id = models.AutoField (u 'serial number', primary_key=True,auto_created=True)
Name = models.CharField (u 'name', max_length=100)
Published = models.DateField (u 'release time', auto_now_add=True)
Def _ str__ (self):
Return self.name
Class Meta:
Verbose_name = 'column'
Verbose_name_plural = 'column list'
Ordering = ['id'] # which column is sorted by
Class Article (models.Model):
Id = models.AutoField (u 'serial number', primary_key=True,auto_created=True)
Title = models.CharField (u' title', max_length=100,default='')
Content = models.TextField (u 'content', default='')
Column = models.ManyToManyField (Column,verbose_name=' ownership column')
Published = models.DateField (u 'release time', auto_now_add=True,null=True)
Def _ str__ (self):
Return self.title
Class Meta:
Verbose_name = 'article'
Verbose_name_plural = 'article list'
Ordering = ['id'] # which articles are sorted by
2. The intermediate table pyclub_article_column is generated
+-+
| | id | article_id | column_id | |
+-+
| | 370 | 411 | 146 | |
| | 371 | 412 | 146 | |
| | 372 | 413 | 165 | |
| | 373 | 414 | 158 | |
| | 374 | 415 | 151 | |
3. I want to get all the data lists in the corresponding column list through column_id. I never knew how to use the intermediate table. Now I understand that the result set column itself can also be used as an object, so the problem is simple.
List_info = Article.objects.filter (column=id)
Although there is no column in the article table, the correspondence has been established in django model.py through many to many, so in view.py, when you use article objects, you can directly use filter for category queries.
The above is what is the method of Django ManyToManyField query across intermediate tables, have you learned the 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.