Update row (SQLAlchemy) with data from marshmallow

前端 未结 5 1846
天命终不由人
天命终不由人 2020-12-29 05:29

I\'m using Flask, Flask-SQLAlchemy, Flask-Marshmallow + marshmallow-sqlalchemy, trying to implement REST api PUT method. I haven\'t found any tutorial using SQLA and Marshma

5条回答
  •  盖世英雄少女心
    2020-12-29 05:43

    Previous answer seems to be outdated as ModelSchema is now deprecated.

    You should instead SQLAlchemyAutoSchema with the proper options.

    class NodeSchema(SQLAlchemyAutoSchema):
        class Meta:
            model = Node
            load_instance = True
            sqla_session = db.session
    
    node_schema = NodeSchema()
    
    # then when you need to update a Node orm instance :
    node_schema.load(node_data, instance=node, partial=True)
    db.session.update()
    

提交回复
热议问题