Flask - 扩展 - SQLAlchemy - 查转换DataFrame
新办法
data = User.query.filter()db.session.commit()data = list(map(userlist, data)) # data就是[{"":"","":""}]的格式def userlist(self): data = {"pk":self.pk, "id":self.id} return data
旧办法
class InvAppService(db.Model): __tablename__ = 'inv_app_service' id = db.Column(db.Integer, primary_key=True) ip = db.Column(db.String(16), unique=True, nullable=False) host = db.Column(db.String(32)) def to_json(self): return { 'ip': self.ip, 'host': self.host, 'LDR': self.app.app_devopses_name().get('LDR'), 'DEV': self.app.app_devopses_name().get('DEV'), 'INF': self.app.app_devopses_name().get('INF'), 'OPS': self.app.app_devopses_name().get('OPS') }
import pandas as pdrtn = []for service in services: rtn.append(service.to_json_ip())rtn = pd.DataFrame(rtn)