Home > Backend Development > Python Tutorial > Detailed example of python flask many-to-many table query

Detailed example of python flask many-to-many table query

零下一度
Release: 2017-06-29 15:37:51
Original
2069 people have browsed it

When we study flask, we will inevitably encounter many-to-many table query. I also encountered this problem today. Let me share my ideas to the Script Home platform for your reference

When we are learning flask, we will inevitably encounter many-to-many table queries. I also encountered this problem today. Then I thought about it for a long time. I didn't think of a solution. I tried several methods, but maybe I gave up due to limitations of my thinking. Later, I went to Baidu online, but found that there was still a certain gap between the results from Baidu and what I wanted. So I Based on the ideas I got from Baidu, I also explored my data structure. Let's take a look at how I query it here. First, I will show you a fragment of the database code I wrote, so as to deepen your understanding.


post_class=db.Table('post_class',
  db.Column('post_id',db.Integer(),db.ForeignKey('posts.id')),
  db.Column('classifa_id',db.Integer(),db.ForeignKey('fenlei.id')))
class Post(db.Model):#文章表
  tablename='posts'
  id=db.Column(db.Integer,primary_key=True,autoincrement=True)
  title=db.Column(db.String(255),unique=True)
  text=db.Column(db.Text())
  publish_date=db.Column(db.DateTime,default=datetime.datetime.now())
  user_id=db.Column(db.Integer,db.ForeignKey('users.id'))
  is_recomment=db.Column(db.Boolean,default=False)
  comments = db.relationship(
    'Comment',
    backref='posts',
    lazy='dynamic')
  tag = db.relationship(
    'Tag',
    secondary=posts_tags,
    backref=db.backref('posts', lazy='dynamic')
  )
  classname=db.relationship('Classifa',
    secondary=post_class,
    backref=db.backref('posts'))
  def repr(self):
    return "<Model Post `{}`>".format(self.title)
class Classifa(db.Model):#分类
  tablename=&#39;fenlei&#39;
  id=db.Column(db.Integer(),primary_key=True)
  name=db.Column(db.String(64))
  def repr(self):
    return self.name
Copy after login

There are three tables here. One is a list of articles, and the other is a classification table. Let’s think about it. An article may belong to multiple A category, then a category may also belong to multiple articles. In this way, we must all understand this logic. So, my third table shows the many-to-many relationship, so how do we query it next? In fact, My current need is to find all the articles under a category.

Let’s take a look at my code


data=Classifa.query.filter_by(name=&#39;数据库&#39;).first()
 data_post=data.posts
Copy after login

Here, I Directly find this category from the category, and then query the articles belonging to this category through the third table. In fact, it is very simple here. Maybe it was because my brain was short-circuited at the time. I don’t know what I thought was right. Looking at it now, it is actually It was still so simple, but I overlooked something at the time. Come on, learn on the way forward.

The above is the detailed content of Detailed example of python flask many-to-many table query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template