Python 如何在sqlalchemy中序列化列表对象?
当我选择All to database时,我使用jsonpickle作为序列化程序,当使用jsonpickle作为序列化程序时,我将把列表对象转换为json,我得到以下输出:Python 如何在sqlalchemy中序列化列表对象?,python,django,python-3.x,sqlalchemy,jsonpickle,Python,Django,Python 3.x,Sqlalchemy,Jsonpickle,当我选择All to database时,我使用jsonpickle作为序列化程序,当使用jsonpickle作为序列化程序时,我将把列表对象转换为json,我得到以下输出: {"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_stat
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
代码:
Session = sessionmaker(bind=connect.ConnectorMySql())
ses = Session()
lst = ses.query(pModel).all()
return lst
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
jsonpickle:
serialized_obj = jsonpickle.encode(lst[0])
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
输出:
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
但我需要这种格式的json:
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
{"name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
你认为这项工作更好吗?这不是
jsonpickle
的目的pickle
和jsonpickle
用于将任意Python对象序列化为某种格式,以便以后可以将其反序列化为完全相同的Python对象。它们不用于根据特定模式对对象进行序列化
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
对于您的情况,您必须自己编写序列化逻辑。例如:
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
obj = lst[0]
serialized_obj = {k: getattr(obj, k) for k in ["name", "fullname", "password", "id"]}
您还必须处理任何特殊类型的问题。您甚至可以想象并尝试自动反映模型上的列
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
顺便问一下,我看到的是明文存储的密码吗?这不是
jsonpickle
的用途pickle
和jsonpickle
用于将任意Python对象序列化为某种格式,以便以后可以将其反序列化为完全相同的Python对象。它们不用于根据特定模式对对象进行序列化
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
对于您的情况,您必须自己编写序列化逻辑。例如:
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
obj = lst[0]
serialized_obj = {k: getattr(obj, k) for k in ["name", "fullname", "password", "id"]}
您还必须处理任何特殊类型的问题。您甚至可以想象并尝试自动反映模型上的列
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
顺便问一下,我看到的是以明文形式存储的密码吗?请尝试在SQLAlchemy对象上定义
\uuuu getstate\uuuuu
和\uuu setstate\uuuu
,如下所示
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
def __getstate__(self):
state = {}
for k, v in self.__dict__.items():
if k != '_sa_instance_state':
state[k] = v
return state
def __setstate__(self, d):
self.__dict__ = d
jsonpickle将使用这些来pickle/depickle,并且您不存储\u sa\u instance\u state
,如果我理解正确,这是内部簿记
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
此处有一些相关信息:请尝试在SQLAlchemy对象上定义
\uu getstate\uuuuu
和\uuuu setstate\uuuuu
,如下所示
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
def __getstate__(self):
state = {}
for k, v in self.__dict__.items():
if k != '_sa_instance_state':
state[k] = v
return state
def __setstate__(self, d):
self.__dict__ = d
jsonpickle将使用这些来pickle/depickle,并且您不存储\u sa\u instance\u state
,如果我理解正确,这是内部簿记
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
这里有一些相关信息:您能发布您的代码吗?好的,请再看一看您能发布您的代码吗?好的,请再看一看谢谢您的回答,不,这是示例:)),谢谢您的回答,不,这是示例:),
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}