当前位置:   article > 正文

python flask sqlalchemy model对象转json_python flask-sqlalchemy 对象转为json

python flask-sqlalchemy 对象转为json
  1. def model_to_json_test():
  2. from flask import jsonify
  3. q = db.session.query(WhiteSite).first() # db = SQLAlchemy()
  4. q_dict = serialize(q)
  5. q_json = jsonify(q_dict)
  6. return q_json
  7. def serialize(model):
  8. from sqlalchemy.orm import class_mapper
  9. columns = [c.key for c in class_mapper(model.__class__).columns]
  10. return dict((c, getattr(model, c)) for c in columns)

 

  1. class WhiteSite(db.Model):
  2. __tablename__ = 'WHITE_SITE'
  3. site_name = db.Column(db.String(128), primary_key=True)
  4. comments = db.Column(db.String(1000))
  5. create_time = db.Column(db.DateTime, default=func.now())
  6. create_user = db.Column(db.String(128))
  7. last_modified_time = db.Column(db.DateTime, default=func.now(), onupdate=func.now())
  8. last_modified_user = db.Column(db.String(512))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/181049?site
推荐阅读
相关标签
  

闽ICP备14008679号