Python Peewee 如何使用Mysql的JSON特性?
作者:互联网
peewee 需要支持mysql json 类型
一、基础model
class User(Model):
name = CharField(unique=True)
some_json_data = CharField()
requested_at = DateTimeField(default=datetime.now())
二、自定义字段 json 字段
class JSONField(TextField):
def db_value(self, value):
return json.dumps(value)
def python_value(self, value):
if value is not None:
return json.loads(value)
三、自定义字段 json 字段
class User(Model):
name = CharField(unique=True)
some_json_data = JSONField()
requested_at = DateTimeField(default=datetime.now())
定义一个JSONField自动执行 json 加载/转储的自定义类型
https://stackoverflow.com/questions/40553790/peewee-orm-jsonfield-for-mysql
http://docs.peewee-orm.com/en/latest/peewee/models.html#creating-a-custom-field
标签:自定义,Python,Peewee,json,value,JSON,peewee,CharField,JSONField 来源: https://blog.csdn.net/a6864657/article/details/123079408