Peewee ORM与Marshmallow序列化/反序列化库集成
项目描述
Marshmallow-Peewee
Marshmallow-Peewee -- Peewee ORM与Marshmallow序列化/反序列化库的集成。
要求
- python >= 3.7
安装
marshmallow-peewee应使用pip进行安装
$ pip install marshmallow-peewee
快速入门
import peewee as pw
class Role(pw.Model):
name = pw.CharField(255, default='user')
class User(pw.Model):
created = pw.DateTimeField(default=dt.datetime.now())
name = pw.CharField(255)
title = pw.CharField(127, null=True)
active = pw.BooleanField(default=True)
rating = pw.IntegerField(default=0)
role = pw.ForeignKeyField(Role)
from marshmallow_peewee import ModelSchema
class UserSchema(ModelSchema):
class Meta:
model = User
role = Role.create()
user = User.create(name='Mike', role=role)
result = UserSchema().dump(user)
print(result)
# {'active': True,
# 'created': '2016-03-29T15:27:18.600034+00:00',
# 'id': 1,
# 'name': 'Mike',
# 'rating': 0,
# 'role': 1,
# 'title': None}
result = UserSchema().load(result)
assert isinstance(result, User)
assert result.name == 'Mike'
from marshmallow_peewee import Related
class UserSchema(ModelSchema):
role = Related()
class Meta:
model = User
result = UserSchema().dump(user)
print(result)
# {'active': True,
# 'created': '2016-03-29T15:30:32.767483+00:00',
# 'id': 1,
# 'name': 'Mike',
# 'rating': 0,
# 'role': {'id': 5, 'name': 'user'},
# 'title': None}
result = UserSchema().load(result)
assert isinstance(result, User)
assert isinstance(result.role, Role)
用法
import peewee as pw
class Role(pw.Model):
name = pw.CharField(255, default='user')
class User(pw.Model):
created = pw.DateTimeField(default=dt.datetime.now())
name = pw.CharField(255)
title = pw.CharField(127, null=True)
active = pw.BooleanField(default=True)
rating = pw.IntegerField(default=0)
role = pw.ForeignKeyField(Role)
from marshmallow_peewee import ModelSchema
class UserSchema(ModelSchema):
class Meta:
# model: Bind peewee.Model to the Schema
model = User
# model_converter: Use custom model_converter
# model_converter = marshmallow_peewee.DefaultConverter
# dump_only_pk: Primary key is dump only
# dump_only_pk = True
# string_keys: Convert keys to strings
# string_keys = True
# id_keys: serialize (and deserialize) ForeignKey fields with _id suffix
# id_keys = False
您可以设置marshmallow-peewee
的全局选项
from marshmallow_peewee import setup
setup(id_keys=True, string_keys=False) # Set options for all schemas
class UserSchema(ModelSchema):
# ...
自定义字段转换
from marshmallow_peewee import DefaultConverter
# Customize global
# Serialize boolean as string
DefaultConverter.register(peewee.BooleanField, marshmallow.fields.String)
# Alternative method
@DefaultConverter.register(peewee.BooleanField)
def build_field(field: peewee.Field, opts, **field_params):
return marshmallow.fields.String(**params)
# Customize only for a scheme
class CustomConverter(DefaultConverter):
pass
CustomConverter.register(...)
class CustomSchema(ModelSchema): # may be inherited
class Meta:
model_converter = CustomConverter
错误追踪
如果您有任何建议、错误报告或烦恼,请向https://github.com/klen/marshmallow-peewee/issues的 issue 追踪器报告
贡献
项目开发发生在:https://github.com/klen/marshmallow-peewee
许可证
许可协议:MIT许可证
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
marshmallow_peewee-4.3.1.tar.gz (7.4 kB 查看散列值)