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

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

  继承混入以上类

posted @ 2020-07-13 17:53  John-Python  阅读(1537)  评论(0编辑  收藏  举报