peewee

Reconnecting MySQL on timeout

南笙酒味 提交于 2019-11-30 14:10:57
问题 I have a Python program which runs on background for weeks, and does database queries every once in a while. For that, I am using the ORM peewee (version 2.2.1). I am using MySQL as a backend. Lately I've encountered a recurring problem with accessing the DB, usually after days of running the program. The error which is raised by peewee is peewee.OperationalError: (2006, 'MySQL server has gone away') The traceback is deep in peewee . I post it here, but as my virtualenv makes filenames too

Peewee model to JSON

老子叫甜甜 提交于 2019-11-30 00:06:28
I'm creating an API using peewee as the ORM and I need the ability to convert a peewee model object into a JSON object to send to the user. Does anyone know of a good way to do this? you can do something like that: class MyModel(peewee.Model): def __str__(self): r = {} for k in self._data.keys(): try: r[k] = str(getattr(self, k)) except: r[k] = json.dumps(getattr(self, k)) return str(r) class User(MyModel): email = CharField() status = CharField(default="enabled") firstname = CharField() lastname = CharField() class Meta: database = db coleifer Peewee has a model_to_dict and dict_to_model

Peewee model to JSON

こ雲淡風輕ζ 提交于 2019-11-28 21:03:41
问题 I'm creating an API using peewee as the ORM and I need the ability to convert a peewee model object into a JSON object to send to the user. Does anyone know of a good way to do this? 回答1: you can do something like that: class MyModel(peewee.Model): def __str__(self): r = {} for k in self._data.keys(): try: r[k] = str(getattr(self, k)) except: r[k] = json.dumps(getattr(self, k)) return str(r) class User(MyModel): email = CharField() status = CharField(default="enabled") firstname = CharField()