search

Home  >  Q&A  >  body text

Python中ORM序列化问题

在Python中有什么方法可以对ORM库查询的数据进行序列化操作,主要针对SQLAlchemy或Peewee这2个框架。

大家讲道理大家讲道理2891 days ago265

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 17:58:32

    If you want to serialize json, you can use marshmallow
    With SALAlchemy, it is marshmallow-sqlalchemy

    reply
    0
  • PHPz

    PHPz2017-04-17 17:58:32

    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

    reply
    0
  • Cancelreply