当前位置:Gxlcms > 数据库问题 > Python中 将sqlalchemy中查询结果转换为字典

Python中 将sqlalchemy中查询结果转换为字典

时间:2021-07-01 10:21:17 帮助过:178人阅读

class MixToJson:
    def single_to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    def dobule_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self, key) is not None:
                result[key] = str(getattr(self, key))
            else:
                result[key] = getattr(self, key)
        return result

    @staticmethod
    def to_json(all_vendors):
        v = [ven.dobule_to_dict() for ven in all_vendors]
        return v

  继承混入以上类

Python中 将sqlalchemy中查询结果转换为字典

标签:highlight   map   sel   end   sql   class   self   attr   查询   

人气教程排行