zoukankan      html  css  js  c++  java
  • sqlalchemy多对多关联

    sqlalchemy_many_to_many.py

    #!-*-coding:utf-8-*-
    from sqlalchemy import Table,Column,Integer,String,DATE,ForeignKey
    from sqlalchemy.orm import relationship
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    base=declarative_base()

    book_m2m_author=Table('book_m2m_author',base.metadata,
                         Column('book_id',Integer,ForeignKey('books.id')),
                         Column('author_id',Integer,ForeignKey('authors.id')))

    class Book(base):
        __tablename__='books'
        id=Column(Integer,primary_key=True)
        name=Column(String(64))
        pub_date=Column(DATE)
        authors=relationship("Author",secondary=book_m2m_author,backref='books')#secondary查第三张表关联关系 backref Author反查有多少本书
        def __repr__(self):
            return self.name

    class Author(base):
        __tablename__='authors'
        id=Column(Integer,primary_key=True)
        name=Column(String(32))
        def __repr__(self):
            return self.name

    engine=create_engine("mysql+pymysql://root:123456@192.168.0.6/shop_db?charset=utf8",echo=False)#连接数据库 echo=TRUE输出执行过程
    #?charset=utf8可插入汉字
    base.metadata.create_all(engine)#创建表

    sqlalchemy_many_to_many_api.py

    from day12 import sqlalchemy_many_to_mang
    from sqlalchemy.orm import sessionmaker

    Session_class = sessionmaker(bind=sqlalchemy_many_to_mang.engine)  # 创建与数据库的连接session class ,注意,这里返回给session的是个class,不是实例
    session = Session_class()  # 生成session实例 session会话 类似cursor
    '''
    b1=sqlalchemy_many_to_mang.Book(name="leran python with Alex",pub_date="2017-06-25")
    b2=sqlalchemy_many_to_mang.Book(name="leran zhangbility with alex",pub_date="2016-06-25")
    b3=sqlalchemy_many_to_mang.Book(name="leran hook uo girls with alex",pub_date="2015-06-25")
    b4=sqlalchemy_many_to_mang.Book(name="风萧萧兮易水寒",pub_date="2018-06-25")

    a1=sqlalchemy_many_to_mang.Author(name="alex")
    a2=sqlalchemy_many_to_mang.Author(name="jack")
    a3=sqlalchemy_many_to_mang.Author(name="rain")

    b1.authors=[a1,a3]
    b3.authors=[a1,a2,a3]

    session.add_all([b4])
    '''
    author_obj=session.query(sqlalchemy_many_to_mang.Author).filter(sqlalchemy_many_to_mang.Author.name=="alex").first()
    print(author_obj.books)#通过作者取书
    book_obj=session.query(sqlalchemy_many_to_mang.Book).filter(sqlalchemy_many_to_mang.Book.id==2).first()
    print(book_obj.authors)
    #book_obj.authors.remove(author_obj)#给书删除作者
    #session.delete(author_obj)#删除作者
    session.commit()

  • 相关阅读:
    xadmin列表页图片缩放(大图小图切换显示)
    xadmin中添加Action类
    xadmin的模块自动注册(注册版本)
    nginx内容清除
    Invalid template name in 'extends' tag: ''. Got this from the 'base_template' variable.
    django xadmin 导入功能添加
    python将excel读取的日期文本数字转为日期(5位数字时间戳)
    数据库主从设置
    django admin 增加查看权限
    oss图片上传失败
  • 原文地址:https://www.cnblogs.com/leiwenbin627/p/10647898.html
Copyright © 2011-2022 走看看