Peewee integration with the marshmallow (de)serialization library.
Project description
Marshmallow-Peewee – Peewee integration with the Marshmallow (de)serialization library.
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, errors = 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, errors = UserSchema().load(user)
assert isinstance(result, User)
assert result.name == 'Mike'
from marshmallow_peewee import Related
class UserSchema(ModelSchema):
role = Related()
class Meta:
model = User
result, errors = 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, errors = UserSchema().load(result)
assert not errors
assert isinstance(result, User)
assert isinstance(result.role, Role)
Requirements
python 2.7+,3.4+
Installation
Peewee-Marshmallow should be installed using pip:
pip install peewee-marshmallow
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
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/peewee-marshmallow/issues
Contributing
Development of The Flask-restler happens at: https://github.com/klen/peewee-marshmallow
Contributors
License
Licensed under a MIT license (See LICENSE)
If you wish to express your appreciation for the project, you are welcome to send a postcard to:
Kirill Klenov pos. Severny 8-3 MO, Istra, 143500 Russia
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
File details
Details for the file Marshmallow-Peewee-0.0.2.tar.gz
.
File metadata
- Download URL: Marshmallow-Peewee-0.0.2.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41c1030a0f78f95dff4b0231c96cd484913debd974f20a48554f25c69ab8288b |
|
MD5 | 9d9ed7963e164ae4253c97f9ad9ad480 |
|
BLAKE2b-256 | e2cfba3333024ecea60d82f62c4b641a17480a6a1737a55fa34b6b2dd6f70e6c |
File details
Details for the file Marshmallow_Peewee-0.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: Marshmallow_Peewee-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2702eb37a52a4166d543bece629c90fb1ed0cd4c5ada0f7ad2c66e4b7c78374b |
|
MD5 | f16030f79a56d0b66c04e13bfa00580e |
|
BLAKE2b-256 | 7f0800fe8cfda3aa0b51539bc1871329dc1bf0c0ea34afaa467d9e452e628f9f |