zoukankan      html  css  js  c++  java
  • 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/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    oracle hint
    oracle资源
    数据迁移相关笔记
    csdn的blog可以直接导入内含图片的word文档吗?
    Windows Live Writer离线博客工具使用教程(适用于博客园、CSDN、51CTO等等博客)
    csdn的博客上传word图片
    怎样将word中的图片插入到CSDN博客中
    测试用Word2007发布博客文章
    用WORD2007发布博客文章
    Word2007发布博客
  • 原文地址:https://www.cnblogs.com/lightsong/p/15633059.html
Copyright © 2011-2022 走看看