• marshmallowsqlalchemy


    marshmallow-sqlalchemy

    https://marshmallow-sqlalchemy.readthedocs.io/en/latest/

    https://github.com/marshmallow-code/marshmallow-sqlalchemy

    SQLAlchemy integration with the marshmallow (de)serialization library.

    marshmallow

    https://marshmallow.readthedocs.io/en/3.0/

    https://github.com/marshmallow-code/marshmallow

    marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes.

    In short, marshmallow schemas can be used to:

    • Validate input data.

    • Deserialize input data to app-level objects.

    • Serialize app-level objects to primitive Python types. The serialized objects can then be rendered to standard formats such as JSON for use in an HTTP API.

    flask_marshmallow

    https://flask-marshmallow.readthedocs.io/en/latest/

    Flask + marshmallow for beautiful APIs

    Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.

    https://realpython.com/flask-connexion-rest-api-part-2/

    https://github.com/realpython/materials/blob/master/flask-connexion-rest-part-2/version_1/models.py

    PLAY CODE

    https://github.com/fanqingsong/code_snippet/blob/master/python/sqlalchemy/alchemy_serialize.py

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    from sqlalchemy import Column, String, create_engine
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
    
    
    # 创建对象的基类:
    Base = declarative_base()
    
    # 定义User对象:
    class User(Base):
        # 表的名字:
        __tablename__ = 'user'
    
        # 表的结构:
        id = Column(String(20), primary_key=True)
        name = Column(String(20))
    
    
    class UserSchema(SQLAlchemyAutoSchema):
        class Meta:
            model = User
            load_instance = True
    
    # 初始化数据库连接:
    engine = create_engine('sqlite:///test1.db')
    
    # Base.metadata.create_all(engine)
    
    
    # 创建DBSession类型:
    DBSession = sessionmaker(bind=engine)
    
    # # 创建session对象:
    # session = DBSession()
    # # 创建新User对象:
    # new_user = User(id='7', name='Bob')
    # # 添加到session:
    # session.add(new_user)
    # # 提交即保存到数据库:
    # session.commit()
    # # 关闭session:
    # session.close()
    
    # 创建Session:
    session = DBSession()
    # 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行:
    user = session.query(User).filter(User.id=='6').one()
    # 打印类型和对象的name属性:
    print('type:', type(user))
    print('name:', user.name)
    # 关闭Session:
    session.close()
    
    user_schema = UserSchema()
    dump_data = user_schema.dump(user)
    print(dump_data)
    
    
    load_data = user_schema.load(dump_data, session=session)
    print(load_data)
    出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    【渗透攻防】深入了解Windows
    浅析B/S架构数据库连接方式
    剖析泄露你银行卡密码的钓鱼网站:真假“10086”
    红黑树的删除和加入节点
    Unity里包裹Debug,且不影响Debug的重定向
    Java向上转型和向下转型(附具体样例)
    Javascript 方法apply和call的差别
    普通androidproject转换为C/C++project之后,再还原成androidproject的解决方式
    初识双网卡
    js面向对象编程: js类定义函数时prototype和this差别?
  • 原文地址:https://www.cnblogs.com/lightsong/p/15633059.html
Copyright © 2020-2023  润新知