Peewee integration with the Marshmallow (de)serialization library.
Project description
Marshmallow-Peewee
Marshmallow-Peewee -- Peewee ORM integration with the Marshmallow (de)serialization library.
Requirements
- python >= 3.7
Installation
marshmallow-peewee should be installed using pip:
$ pip install marshmallow-peewee
Quickstart
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)
Usage
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.ModelConverter
# 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
You may set global options for marshmallow-peewee
:
from marshmallow_peewee import setup
setup(id_keys=True, string_keys=False) # Set options for all schemas
class UserSchema(ModelSchema):
# ...
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/marshmallow-peewee/issues
Contributing
Development of the project happens at: https://github.com/klen/marshmallow-peewee
License
Licensed under a MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for Marshmallow_Peewee-3.4.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54d463f371d2e215a3dc5715033ff73bb75185614c9fb5fe26333e659ad2ab4b |
|
MD5 | 975f26e9bcde4f4def258240354e6478 |
|
BLAKE2b-256 | 87b540783ed097b411faa64d02cc86ace9442b54c068112afbace3f298794b23 |