在Python中有什么方法可以对ORM库查询的数据进行序列化操作,主要针对SQLAlchemy或Peewee这2个框架。
光阴似箭催人老,日月如移越少年。
If you want to serialize json, you can use marshmallowWith SALAlchemy, it is marshmallow-sqlalchemy
We use pickle for serialization when using sqlalchemy. When the front end passes it back directly, we can directly obtain the object without having to go to the database to obtain the object again. The code is as follows:
251 252 def __str__(self): 253 return base64.urlsafe_b64encode(pickle.dumps(self)) .decode('ascii') 254 255 @staticmethod 256 def fromstr(ss): 257 if isinstance(ss, bytes): 258 ss = ss.encode('ascii') 259 obj = pickle.loads(base64.urlsafe_b64decode(ss)) 260 return obj
If you want to serialize json, you can use marshmallow
With SALAlchemy, it is marshmallow-sqlalchemy
We use pickle for serialization when using sqlalchemy. When the front end passes it back directly, we can directly obtain the object without having to go to the database to obtain the object again. The code is as follows: