flask ORM: Flask-SQLAlchemy【多表多对多】增删改查
案例表:
1 # coding=utf-8 2 from flask_sqlalchemy import SQLAlchemy 3 4 db = SQLAlchemy() 5 6 7 tags_class = db.Table('tags_classifications', 8 db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')), 9 db.Column('classify_id', db.Integer, db.ForeignKey('classification.id')) 10 ) 11 12 class Classification(db.Model): 13 __tablename__ = 'classification' 14 id = db.Column(db.Integer, primary_key=True) 15 title = db.Column(db.String(1024), unique=True) 16 img_title = db.Column(db.String(1024)) 17 img_url = db.Column(db.String(2048)) 18 19 tags = db.relationship('Tag', secondary=tags_class, 20 backref=db.backref('classifications', lazy='dynamic')) 21 22 23 def __init__(self, title,img_title,img_url): 24 self.title = title 25 self.img_title = img_title 26 self.img_url = img_url 27 28 def __repr__(self): 29 return '<Classification %r>' % self.title 30 31 class Tag(db.Model): 32 __tablename__ = 'tag' 33 id = db.Column(db.Integer, primary_key=True) 34 name = db.Column(db.String(1024), unique=True) 35 img_title = db.Column(db.String(1024)) 36 img_url = db.Column(db.String(2048)) 37 38 39 def __init__(self, name,img_title,img_url): 40 self.name = name 41 self.img_title = img_title 42 self.img_url = img_url 43 44 def __repr__(self): 45 return '<Tag: %r>' % self.name
flask 路由表插入数据:
1 # coding=utf-8 2 import os 3 import random 4 from flask import Blueprint, request, session 5 from flask import render_template, redirect, jsonify 6 from shengyuanapp.models import db, Classification,Tag 7 bp = Blueprint(__name__, 'home') 8 9 10 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 11 img_path = os.path.join(base_dir,"static","img","bmp") 12 img_full_path = [] 13 classify_obj_list = [] 14 tags_obj_list = [] 15 16 17 list_img = os.listdir(img_path) 18 for i in list_img: 19 tmp_dir = os.path.join(img_path,i) 20 img_full_path.append(tmp_dir) 21 22 23 def gen_tags(): 24 """ 25 26 :return: 27 """ 28 tags_list = [] 29 for i in img_full_path: 30 tag_name = "tag_" + str(img_full_path.index(i)) 31 img_title = "img_%s" % (img_full_path.index(i)) 32 img_url = i 33 tag = Tag(name=tag_name,img_title=img_title,img_url=img_url) 34 tags_list.append(tag) 35 return tags_list 36 37 def gen_classifications(): 38 """ 39 40 :return: 41 """ 42 pages_list = [] 43 tags = gen_tags() 44 for i in img_full_path: 45 title = "calssification" + str(img_full_path.index(i)) 46 img_title = "img_%s" % (img_full_path.index(i)) 47 img_url = i 48 page_obj = Classification(title=title,img_title=img_title,img_url=img_url) 49 page_obj.tags = tags 50 pages_list.append(page_obj) 51 52 return pages_list 53 54 55 @bp.route('/', methods=('GET', 'POST')) 56 def home(): 57 58 return render_template('index.html') 59 60 @bp.route('/insert_img',methods=('GET', 'POST')) 61 def insert_img(): 62 63 classes = gen_classifications() # 生成的 gen_classification 对象列表。 64 db.session.add_all(classes) # 一次性添加所有 gen_classification。 65 db.session.commit() # 提交到数据库
flask 路由表查询:
1 @bp.route('/query_img', methods=('GET', 'POST')) 2 def query_img(): 3 obj_id = random.choice(list(range(10))) 4 tag_name = "tag_" + str(obj_id) 5 tag = Tag.query.filter_by(name=tag_name).first() 6 print("tag: ", tag) 7 8 classi_title = "calssification" + str(obj_id) 9 classi = Classification.query.filter_by(title=classi_title).first() 10 print("classi", classi.tags)