如何将 SqlAlchemy 结果序列化为 JSON?
- 2024-12-13 08:36:00
- admin 原创
- 150
问题描述:
Django 有一些很好的将从 DB 返回的 ORM 模型自动序列化为 JSON 格式的功能。
如何将 SQLAlchemy 查询结果序列化为 JSON 格式?
我试过了jsonpickle.encode
,但它对查询对象本身进行了编码。我试过了json.dumps(items)
,但它返回
TypeError: <Product('3', 'some name', 'some desc')> is not JSON serializable
将 SQLAlchemy ORM 对象序列化为 JSON /XML 真的那么难吗?没有默认的序列化程序吗?如今,序列化 ORM 查询结果是一项非常常见的任务。
我需要的只是返回 SQLAlchemy 查询结果的 JSON 或 XML 数据表示。
需要在 javascript 数据网格 (JQGrid http://www.trirand.com/blog/ )中使用 JSON/XML 格式的 SQLAlchemy 对象查询结果
解决方案 1:
您可以将对象输出为字典:
class User:
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
然后你使用它User.as_dict()
来序列化你的对象。
如如何将 SQLAlchemy 行对象转换为 Python 字典?中所述
解决方案 2:
Python 3.7+ 和 Flask 1.1+ 可以使用内置的dataclasses包
from dataclasses import dataclass
from datetime import datetime
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
@dataclass
class User(db.Model):
id: int
email: str
id = db.Column(db.Integer, primary_key=True, auto_increment=True)
email = db.Column(db.String(200), unique=True)
@app.route('/users/')
def users():
users = User.query.all()
return jsonify(users)
if __name__ == "__main__":
users = User(email="user1@gmail.com"), User(email="user2@gmail.com")
db.create_all()
db.session.add_all(users)
db.session.commit()
app.run()
该/users/
路线现在将返回用户列表。
[
{"email": "user1@gmail.com", "id": 1},
{"email": "user2@gmail.com", "id": 2}
]
自动序列化相关模型
@dataclass
class Account(db.Model):
id: int
users: User
id = db.Column(db.Integer)
users = db.relationship(User) # User model would need a db.ForeignKey field
的回应jsonify(account)
是这样的。
{
"id":1,
"users":[
{
"email":"user1@gmail.com",
"id":1
},
{
"email":"user2@gmail.com",
"id":2
}
]
}
覆盖默认的 JSON 编码器
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
"Add support for serializing timedeltas"
def default(o):
if type(o) == datetime.timedelta:
return str(o)
if type(o) == datetime.datetime:
return o.isoformat()
return super().default(o)
app.json_encoder = CustomJSONEncoder
解决方案 3:
扁平化实施
你可以使用这样的方法:
from sqlalchemy.ext.declarative import DeclarativeMeta
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# an SQLAlchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
data = obj.__getattribute__(field)
try:
json.dumps(data) # this will fail on non-encodable values, like other classes
fields[field] = data
except TypeError:
fields[field] = None
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)
然后使用以下命令转换为 JSON:
c = YourAlchemyClass()
print json.dumps(c, cls=AlchemyEncoder)
它将忽略不可编码的字段(将它们设置为“无”)。
它不会自动扩展关系(因为这可能导致自我引用,并永远循环)。
递归、非循环实现
但是,如果您希望永远循环,则可以使用:
from sqlalchemy.ext.declarative import DeclarativeMeta
def new_alchemy_encoder():
_visited_objs = []
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# don't re-visit self
if obj in _visited_objs:
return None
_visited_objs.append(obj)
# an SQLAlchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
fields[field] = obj.__getattribute__(field)
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)
return AlchemyEncoder
然后使用以下方法对对象进行编码:
print json.dumps(e, cls=new_alchemy_encoder(), check_circular=False)
这将对所有孩子、他们所有的孩子以及他们所有的孩子进行编码……基本上,可能会对整个数据库进行编码。当它遇到之前编码过的内容时,它会将其编码为“无”。
递归的、可能循环的、选择性的实现
另一种可能更好的选择是能够指定要扩展的字段:
def new_alchemy_encoder(revisit_self = False, fields_to_expand = []):
_visited_objs = []
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# don't re-visit self
if revisit_self:
if obj in _visited_objs:
return None
_visited_objs.append(obj)
# go through each field in this SQLalchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
val = obj.__getattribute__(field)
# is this field another SQLalchemy object, or a list of SQLalchemy objects?
if isinstance(val.__class__, DeclarativeMeta) or (isinstance(val, list) and len(val) > 0 and isinstance(val[0].__class__, DeclarativeMeta)):
# unless we're expanding this field, stop here
if field not in fields_to_expand:
# not expanding this field: set it to None and continue
fields[field] = None
continue
fields[field] = val
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)
return AlchemyEncoder
现在你可以这样调用它:
print json.dumps(e, cls=new_alchemy_encoder(False, ['parents']), check_circular=False)
例如,仅扩展名为“parents”的 SQLAlchemy 字段。
解决方案 4:
您可以将 RowProxy 转换为字典,如下所示:
d = dict(row.items())
然后将其序列化为 JSON(您必须为datetime
值之类的内容指定一个编码器)如果您只想要一条记录(而不是相关记录的完整层次结构),那么这并不难。
json.dumps([(dict(row.items())) for row in rs])
解决方案 5:
我建议使用marshmallow。它允许您创建序列化器来表示模型实例,并支持关系和嵌套对象。
以下是他们文档中的一个截断示例。以 ORM 模型为例Author
:
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
first = db.Column(db.String(80))
last = db.Column(db.String(80))
该类的 Marshmallow 模式构建如下:
class AuthorSchema(Schema):
id = fields.Int(dump_only=True)
first = fields.Str()
last = fields.Str()
formatted_name = fields.Method("format_name", dump_only=True)
def format_name(self, author):
return "{}, {}".format(author.last, author.first)
...用法如下:
author_schema = AuthorSchema()
author_schema.dump(Author.query.first())
...会产生如下输出:
{
"first": "Tim",
"formatted_name": "Peters, Tim",
"id": 1,
"last": "Peters"
}
看一下他们的完整Flask-SQLAlchemy 示例。
一个名为的库marshmallow-sqlalchemy
专门集成了 SQLAlchemy 和 marshmallow。在该库中,上面描述的模型的架构Author
如下所示:
class AuthorSchema(ModelSchema):
class Meta:
model = Author
该集成允许从 SQLAlchemy 类型推断字段类型Column
。
marshmallow-sqlalchemy 在这里。
解决方案 6:
您可以像这样使用 SqlAlchemy 的自省:
mysql = SQLAlchemy()
from sqlalchemy import inspect
class Contacts(mysql.Model):
__tablename__ = 'CONTACTS'
id = mysql.Column(mysql.Integer, primary_key=True)
first_name = mysql.Column(mysql.String(128), nullable=False)
last_name = mysql.Column(mysql.String(128), nullable=False)
phone = mysql.Column(mysql.String(128), nullable=False)
email = mysql.Column(mysql.String(128), nullable=False)
street = mysql.Column(mysql.String(128), nullable=False)
zip_code = mysql.Column(mysql.String(128), nullable=False)
city = mysql.Column(mysql.String(128), nullable=False)
def toDict(self):
return { c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs }
@app.route('/contacts',methods=['GET'])
def getContacts():
contacts = Contacts.query.all()
contactsArr = []
for contact in contacts:
contactsArr.append(contact.toDict())
return jsonify(contactsArr)
@app.route('/contacts/<int:id>',methods=['GET'])
def getContact(id):
contact = Contacts.query.get(id)
return jsonify(contact.toDict())
从这里的答案中获得启发:
将 sqlalchemy 行对象转换为 python dict
解决方案 7:
出于安全原因,您永远不应返回模型的所有字段。我更喜欢有选择地选择它们。
Flask 的 json 编码现在支持 UUID、datetime 和 relationships(并为 flask_sqlalchemy类添加了query
and )。我已更新编码器如下:query_class
`db.Model`
应用程序/json_encoder.py
from sqlalchemy.ext.declarative import DeclarativeMeta
from flask import json
class AlchemyEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o.__class__, DeclarativeMeta):
data = {}
fields = o.__json__() if hasattr(o, '__json__') else dir(o)
for field in [f for f in fields if not f.startswith('_') and f not in ['metadata', 'query', 'query_class']]:
value = o.__getattribute__(field)
try:
json.dumps(value)
data[field] = value
except TypeError:
data[field] = None
return data
return json.JSONEncoder.default(self, o)
app/__init__.py
# json encoding
from app.json_encoder import AlchemyEncoder
app.json_encoder = AlchemyEncoder
通过这个,我可以选择性地添加一个__json__
属性,该属性返回我想要编码的字段列表:
app/models.py
class Queue(db.Model):
id = db.Column(db.Integer, primary_key=True)
song_id = db.Column(db.Integer, db.ForeignKey('song.id'), unique=True, nullable=False)
song = db.relationship('Song', lazy='joined')
type = db.Column(db.String(20), server_default=u'audio/mpeg')
src = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, server_default=db.func.now())
updated_at = db.Column(db.DateTime, server_default=db.func.now(), onupdate=db.func.now())
def __init__(self, song):
self.song = song
self.src = song.full_path
def __json__(self):
return ['song', 'src', 'type', 'created_at']
我将@jsonapi添加到我的视图,返回结果列表,然后我的输出如下:
[
{
"created_at": "Thu, 23 Jul 2015 11:36:53 GMT",
"song":
{
"full_path": "/static/music/Audioslave/Audioslave [2002]/1 Cochise.mp3",
"id": 2,
"path_name": "Audioslave/Audioslave [2002]/1 Cochise.mp3"
},
"src": "/static/music/Audioslave/Audioslave [2002]/1 Cochise.mp3",
"type": "audio/mpeg"
}
]
解决方案 8:
以下是使 SqlAlchemy 对象可序列化的一种方法:实现自定义JSONEncoder
并将其添加到基类:
用法:
from sqlalchemy.ext.declarative import declarative_base
from flask.ext.jsontools import JsonSerializableBase
Base = declarative_base(cls=(DynamicJSONEncoder,))
class User(Base):
#...
现在User
模型已经神奇地可序列化了。
这是实现自定义 JSONEncoder 的一种方法:通过实现该方法,您可以自定义实例在 JSON 中的表示方式__json__()
:
from flask.json import JSONEncoder
class DynamicJSONEncoder(JSONEncoder):
""" JSON encoder for custom classes:
Uses __json__() method if available to prepare the object.
Especially useful for SQLAlchemy models
"""
def default(self, o):
# Custom JSON-encodeable objects
if hasattr(o, '__json__'):
return o.__json__()
# Default
return super().default(o)
解决方案 9:
更详细的解释。在您的模型中,添加:
def as_dict(self):
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
这str()
是针对 Python 3 的,因此如果使用 Python 2,请使用unicode()
。它应该有助于反序列化日期。如果不处理这些,您可以将其删除。
您现在可以像这样查询数据库
some_result = User.query.filter_by(id=current_user.id).first().as_dict()
First()
是为了避免奇怪的错误。as_dict()
现在将反序列化结果。反序列化后,就可以将其转换为 json 了
jsonify(some_result)
解决方案 10:
虽然原始问题可以追溯到一段时间,但这里的答案数量(以及我自己的经验)表明这是一个非平凡的问题,涉及许多不同复杂程度和不同权衡的方法。
这就是我构建SQLAthanor库的原因,该库扩展了 SQLAlchemy 的声明式 ORM,并提供可配置的序列化/反序列化支持,您可能需要了解一下。
该库支持:
Python 2.7、3.4、3.5 和 3.6。
SQLAlchemy 0.9 及更高版本
序列化/反序列化 JSON、CSV、YAML 和 Python
dict
列/属性、关系、混合属性和关联代理的序列化/反序列化
启用和禁用特定格式和列/关系/属性的序列化(例如,您想要支持入站
password
值,但从不包括出站值)序列化前和反序列化后的值处理(用于验证或类型强制)
语法非常简单,既符合 Python 风格,又与 SQLAlchemy 自身方法无缝一致
您可以在这里查看(我希望!)综合文档:https: //sqlathanor.readthedocs.io/en/latest
希望这有帮助!
解决方案 11:
自定义序列化和反序列化。
“from_json”(类方法)根据json数据构建Model对象。
“deserialize”只能在实例上调用,并将来自 json 的所有数据合并到模型实例中。
“序列化” ——递归序列化
需要__write_only__属性来定义只写属性(例如“password_hash”)。
class Serializable(object):
__exclude__ = ('id',)
__include__ = ()
__write_only__ = ()
@classmethod
def from_json(cls, json, selfObj=None):
if selfObj is None:
self = cls()
else:
self = selfObj
exclude = (cls.__exclude__ or ()) + Serializable.__exclude__
include = cls.__include__ or ()
if json:
for prop, value in json.iteritems():
# ignore all non user data, e.g. only
if (not (prop in exclude) | (prop in include)) and isinstance(
getattr(cls, prop, None), QueryableAttribute):
setattr(self, prop, value)
return self
def deserialize(self, json):
if not json:
return None
return self.__class__.from_json(json, selfObj=self)
@classmethod
def serialize_list(cls, object_list=[]):
output = []
for li in object_list:
if isinstance(li, Serializable):
output.append(li.serialize())
else:
output.append(li)
return output
def serialize(self, **kwargs):
# init write only props
if len(getattr(self.__class__, '__write_only__', ())) == 0:
self.__class__.__write_only__ = ()
dictionary = {}
expand = kwargs.get('expand', ()) or ()
prop = 'props'
if expand:
# expand all the fields
for key in expand:
getattr(self, key)
iterable = self.__dict__.items()
is_custom_property_set = False
# include only properties passed as parameter
if (prop in kwargs) and (kwargs.get(prop, None) is not None):
is_custom_property_set = True
iterable = kwargs.get(prop, None)
# loop trough all accessible properties
for key in iterable:
accessor = key
if isinstance(key, tuple):
accessor = key[0]
if not (accessor in self.__class__.__write_only__) and not accessor.startswith('_'):
# force select from db to be able get relationships
if is_custom_property_set:
getattr(self, accessor, None)
if isinstance(self.__dict__.get(accessor), list):
dictionary[accessor] = self.__class__.serialize_list(object_list=self.__dict__.get(accessor))
# check if those properties are read only
elif isinstance(self.__dict__.get(accessor), Serializable):
dictionary[accessor] = self.__dict__.get(accessor).serialize()
else:
dictionary[accessor] = self.__dict__.get(accessor)
return dictionary
解决方案 12:
使用SQLAlchemy 中的内置序列化器:
from sqlalchemy.ext.serializer import loads, dumps
obj = MyAlchemyObject()
# serialize object
serialized_obj = dumps(obj)
# deserialize object
obj = loads(serialized_obj)
如果要在会话之间传输对象,请记住使用 从当前会话中分离对象session.expunge(obj)
。要再次附加它,只需执行session.add(obj)
。
解决方案 13:
这里有一个解决方案,可让您选择要包含在输出中的关系,深度可想而知。注意:这是完全重写,以 dict/str 作为参数而不是列表。修复了一些问题。
def deep_dict(self, relations={}):
"""Output a dict of an SA object recursing as deep as you want.
Takes one argument, relations which is a dictionary of relations we'd
like to pull out. The relations dict items can be a single relation
name or deeper relation names connected by sub dicts
Example:
Say we have a Person object with a family relationship
person.deep_dict(relations={'family':None})
Say the family object has homes as a relation then we can do
person.deep_dict(relations={'family':{'homes':None}})
OR
person.deep_dict(relations={'family':'homes'})
Say homes has a relation like rooms you can do
person.deep_dict(relations={'family':{'homes':'rooms'}})
and so on...
"""
mydict = dict((c, str(a)) for c, a in
self.__dict__.items() if c != '_sa_instance_state')
if not relations:
# just return ourselves
return mydict
# otherwise we need to go deeper
if not isinstance(relations, dict) and not isinstance(relations, str):
raise Exception("relations should be a dict, it is of type {}".format(type(relations)))
# got here so check and handle if we were passed a dict
if isinstance(relations, dict):
# we were passed deeper info
for left, right in relations.items():
myrel = getattr(self, left)
if isinstance(myrel, list):
mydict[left] = [rel.deep_dict(relations=right) for rel in myrel]
else:
mydict[left] = myrel.deep_dict(relations=right)
# if we get here check and handle if we were passed a string
elif isinstance(relations, str):
# passed a single item
myrel = getattr(self, relations)
left = relations
if isinstance(myrel, list):
mydict[left] = [rel.deep_dict(relations=None)
for rel in myrel]
else:
mydict[left] = myrel.deep_dict(relations=None)
return mydict
因此,以 person/family/homes/rooms 为例...将其转换为 json,您需要做的就是
json.dumps(person.deep_dict(relations={'family':{'homes':'rooms'}}))
解决方案 14:
step1:
class CNAME:
...
def as_dict(self):
return {item.name: getattr(self, item.name) for item in self.__table__.columns}
step2:
list = []
for data in session.query(CNAME).all():
list.append(data.as_dict())
step3:
return jsonify(list)
解决方案 15:
尽管这是一篇老帖子,但也许我没有回答上述问题,但我想谈谈我的序列化,至少它对我有用。
我使用FastAPI,SqlAlchemy和MySQL,但不使用orm模型;
# from sqlalchemy import create_engine
# from sqlalchemy.orm import sessionmaker
# engine = create_engine(config.SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
序列化代码
import decimal
import datetime
def alchemy_encoder(obj):
"""JSON encoder function for SQLAlchemy special classes."""
if isinstance(obj, datetime.date):
return obj.strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(obj, decimal.Decimal):
return float(obj)
import json
from sqlalchemy import text
# db is SessionLocal() object
app_sql = 'SELECT * FROM app_info ORDER BY app_id LIMIT :page,:page_size'
# The next two are the parameters passed in
page = 1
page_size = 10
# execute sql and return a <class 'sqlalchemy.engine.result.ResultProxy'> object
app_list = db.execute(text(app_sql), {'page': page, 'page_size': page_size})
# serialize
res = json.loads(json.dumps([dict(r) for r in app_list], default=alchemy_encoder))
如果不起作用,请忽略我的回答。我参考了这里
https://codeandlife.com/2014/12/07/sqlalchemy-results-to-json-the-easy-way/
解决方案 16:
安装 simplejsonpip install simplejson
并创建一个类
class Serialise(object):
def _asdict(self):
"""
Serialization logic for converting entities using flask's jsonify
:return: An ordered dictionary
:rtype: :class:`collections.OrderedDict`
"""
result = OrderedDict()
# Get the columns
for key in self.__mapper__.c.keys():
if isinstance(getattr(self, key), datetime):
result["x"] = getattr(self, key).timestamp() * 1000
result["timestamp"] = result["x"]
else:
result[key] = getattr(self, key)
return result
并将该类继承到每个 ORM 类,这样该_asdict
函数就可以注册到每个 ORM 类,然后就可以使用了。然后在任何地方都可以使用 jsonify
解决方案 17:
2023 年将推出 Python 3.7+
您可以将dataclass
装饰器添加到模型中并定义自定义 JSON 序列化器,然后json.dumps
就可以工作了(通过向 提供自定义编码器cls
)。在下面的示例中,db_row
是DB
该类的一个实例:
json.dumps(db_row, cls=models.CustomJSONEncoder)
{"id": 25, "name": "A component", "author": "Bob", "modified": "2023-02-08T11:49:15.675837"}
自定义 JSON 序列化器可以轻松修改,以使其与任何非本机 JSON 可序列化的类型兼容。
模型.py
from datetime import datetime
import dataclasses
import json
from sqlalchemy import Column, Integer, String, DateTime
from database import Base
@dataclasses.dataclass # <<-- add this decorator
class DB(Base):
"""Model used for SQLite database entries."""
__tablename__ = "components"
id: int = Column(Integer, primary_key=True, index=True)
name: str = Column(String)
author: str = Column(String)
modified: datetime = Column(DateTime(timezone=True), default=datetime.utcnow)
class CustomJSONEncoder(json.JSONEncoder): # <<-- Add this custom encoder
"""Custom JSON encoder for the DB class."""
def default(self, o):
if dataclasses.is_dataclass(o): # this serializes anything dataclass can handle
return dataclasses.asdict(o)
if isinstance(o, datetime): # this adds support for datetime
return o.isoformat()
return super().default(o)
为了进一步扩展它以适应您可能在数据库中使用的任何不可序列化的类型,请if
向返回可序列化内容的自定义编码器类添加另一个语句(例如str
)。
解决方案 18:
这并不那么简单。我写了一些代码来实现这一点。我仍在研究它,它使用了 MochiKit 框架。它基本上使用代理和注册的 JSON 转换器在 Python 和 Javascript 之间转换复合对象。
数据库对象的浏览器端是db.js它需要proxy.js
中的基本 Python 代理源。
在 Python 方面,有基本的代理模块。最后是webserver.py中的 SqlAlchemy 对象编码器。它还依赖于models.py文件中找到的元数据提取器。
解决方案 19:
def alc2json(row):
return dict([(col, str(getattr(row,col))) for col in row.__table__.columns.keys()])
我想用这个来玩一点代码高尔夫。
仅供参考:我使用automap_base,因为我们有一个根据业务需求单独设计的模式。我今天才开始使用 SQLAlchemy,但文档指出 automap_base 是 declarative_base 的扩展,这似乎是 SQLAlchemy ORM 中的典型范例,所以我相信这应该可行。
它不会像Tjorriemorrie的解决方案那样花哨地遵循外键,而是简单地将列与值匹配,并通过对列值进行 str() 来处理 Python 类型。我们的值由 Python datetime.time 和 decimal.Decimal 类类型结果组成,因此它可以完成工作。
希望这对任何路人都有帮助!
解决方案 20:
我知道这是一篇很老的帖子。我采用了@SashaB 提供的解决方案,并根据我的需要进行了修改。
我添加了以下内容:
字段忽略列表:序列化时要忽略的字段列表
字段替换列表:包含在序列化时要被值替换的字段名称的字典。
删除方法并使 BaseQuery 序列化
我的代码如下:
def alchemy_json_encoder(revisit_self = False, fields_to_expand = [], fields_to_ignore = [], fields_to_replace = {}):
"""
Serialize SQLAlchemy result into JSon
:param revisit_self: True / False
:param fields_to_expand: Fields which are to be expanded for including their children and all
:param fields_to_ignore: Fields to be ignored while encoding
:param fields_to_replace: Field keys to be replaced by values assigned in dictionary
:return: Json serialized SQLAlchemy object
"""
_visited_objs = []
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# don't re-visit self
if revisit_self:
if obj in _visited_objs:
return None
_visited_objs.append(obj)
# go through each field in this SQLalchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata' and x not in fields_to_ignore]:
val = obj.__getattribute__(field)
# is this field method defination, or an SQLalchemy object
if not hasattr(val, "__call__") and not isinstance(val, BaseQuery):
field_name = fields_to_replace[field] if field in fields_to_replace else field
# is this field another SQLalchemy object, or a list of SQLalchemy objects?
if isinstance(val.__class__, DeclarativeMeta) or \n (isinstance(val, list) and len(val) > 0 and isinstance(val[0].__class__, DeclarativeMeta)):
# unless we're expanding this field, stop here
if field not in fields_to_expand:
# not expanding this field: set it to None and continue
fields[field_name] = None
continue
fields[field_name] = val
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)
return AlchemyEncoder
希望它能帮助到别人!
解决方案 21:
以下代码将把 sqlalchemy 结果序列化为 json。
import json
from collections import OrderedDict
def asdict(self):
result = OrderedDict()
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
def to_array(all_vendors):
v = [ ven.asdict() for ven in all_vendors ]
return json.dumps(v)
呼唤乐趣,
def all_products():
all_products = Products.query.all()
return to_array(all_products)
解决方案 22:
AlchemyEncoder 非常棒,但有时无法处理十进制值。下面是一个改进的编码器,可以解决十进制问题 -
class AlchemyEncoder(json.JSONEncoder):
# To serialize SQLalchemy objects
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
model_fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
data = obj.__getattribute__(field)
print data
try:
json.dumps(data) # this will fail on non-encodable values, like other classes
model_fields[field] = data
except TypeError:
model_fields[field] = None
return model_fields
if isinstance(obj, Decimal):
return float(obj)
return json.JSONEncoder.default(self, obj)
解决方案 23:
当使用 sqlalchemy 连接到数据库时,这是一个高度可配置的简单解决方案。使用 pandas。
import pandas as pd
import sqlalchemy
#sqlalchemy engine configuration
engine = sqlalchemy.create_engine....
def my_function():
#read in from sql directly into a pandas dataframe
#check the pandas documentation for additional config options
sql_DF = pd.read_sql_table("table_name", con=engine)
# "orient" is optional here but allows you to specify the json formatting you require
sql_json = sql_DF.to_json(orient="index")
return sql_json
解决方案 24:
(对Sasha B 的精彩回答做了些许修改)
这专门将日期时间对象转换为字符串,在原始答案中将其转换为None
:
# Standard library imports
from datetime import datetime
import json
# 3rd party imports
from sqlalchemy.ext.declarative import DeclarativeMeta
class JsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
dict = {}
# Remove invalid fields and just get the column attributes
columns = [x for x in dir(obj) if not x.startswith("_") and x != "metadata"]
for column in columns:
value = obj.__getattribute__(column)
try:
json.dumps(value)
dict[column] = value
except TypeError:
if isinstance(value, datetime):
dict[column] = value.__str__()
else:
dict[column] = None
return dict
return json.JSONEncoder.default(self, obj)
解决方案 25:
class SqlToDict:
def __init__(self, data) -> None:
self.data = data
def to_timestamp(self, date):
if isinstance(date, datetime):
return int(datetime.timestamp(date))
else:
return date
def to_dict(self) -> List:
arr = []
for i in self.data:
keys = [*i.keys()]
values = [*i]
values = [self.to_timestamp(d) for d in values]
arr.append(dict(zip(keys, values)))
return arr
例如:
SqlToDict(data).to_dict()
解决方案 26:
2023 年很晚
但比 json 方法更快:O[n] 且需要 2n 个空间
我的实现还支持类型检查和隐藏一些可以修剪不必要数据的属性的能力。以及可选的调试标志来检查内容。使用 python 3.10 海象运算符。
def obj_to_dict(obj, remove=['_sa_instance_state'], debug=False):
result = {}
if type(obj).__name__ == "Row":
return dict(obj)
for k in (obj := obj.__dict__):
if k not in remove:
result[key] = obj[k]
print(result) if debug else None
return result
解决方案 27:
as_dict
您可以向模型添加方法
from sqlalchemy.inspection import inspect
def implement_as_dict(model):
if not hasattr(model,"as_dict"):
column_names=[]
imodel = inspect(model)
for c in imodel.columns:
column_names.append(c.key)
#define model.as_dict()
def as_dict(self):
d = {}
for c in column_names:
d[c] = getattr(self,c)
return d
setattr(model,"as_dict",as_dict)
#model definition
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
# adding as_dict definition to model
implement_as_dict(User)
然后你可以使用
user = session.query(User).filter_by(name='rick').first()
user.as_dict()
#sample output
{"id":1,"name":"rick"}
解决方案 28:
在 Flask 下,它可以工作并处理数据时间字段,将类型字段转换
'time': datetime.datetime(2018, 3, 22, 15, 40)
为
"time": "2018-03-22 15:40:00"
:
obj = {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
# This to get the JSON body
return json.dumps(obj)
# Or this to get a response object
return jsonify(obj)
解决方案 29:
内置的 utf-8 序列化器无法解码某些输入的无效起始字节。因此,我选择了:
def row_to_dict(row):
temp = row.__dict__
temp.pop('_sa_instance_state', None)
return temp
def rows_to_list(rows):
ret_rows = []
for row in rows:
ret_rows.append(row_to_dict(row))
return ret_rows
@website_blueprint.route('/api/v1/some/endpoint', methods=['GET'])
def some_api():
'''
/some_endpoint
'''
rows = rows_to_list(SomeModel.query.all())
response = app.response_class(
response=jsonplus.dumps(rows),
status=200,
mimetype='application/json'
)
return response
解决方案 30:
也许你可以使用这样的课程
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy import Table
class Custom:
"""Some custom logic here!"""
__table__: Table # def for mypy
@declared_attr
def __tablename__(cls): # pylint: disable=no-self-argument
return cls.__name__ # pylint: disable= no-member
def to_dict(self) -> Dict[str, Any]:
"""Serializes only column data."""
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
Base = declarative_base(cls=Custom)
class MyOwnTable(Base):
#COLUMNS!
所有对象都有to_dict
方法