Skip to main content

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(result)
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 Marshmallow-Peewee 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Marshmallow-Peewee-0.2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

Marshmallow_Peewee-0.2.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Marshmallow-Peewee-0.2.0.tar.gz.

File metadata

File hashes

Hashes for Marshmallow-Peewee-0.2.0.tar.gz
Algorithm Hash digest
SHA256 96a9ef0efba96c205229aff4bebb522c8716d2a17b5d8987122705a34c0e9967
MD5 f022dacaf3e64e74dbecd75e91a0c4f5
BLAKE2b-256 2c1c27c17e3c29e752f7c21c8a1338d24835c0e1a0d87e08a368eb82ee82a8cf

See more details on using hashes here.

File details

Details for the file Marshmallow_Peewee-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Marshmallow_Peewee-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2b152cecf2157721c5f65aa7e9845de20e3768527f6f61967ad5ccfad9e720ff
MD5 23b1ea089dd4744d199b681402600148
BLAKE2b-256 c176702cbbd6f05ce6b3230d092772bb4abbff59b0f7b19942cfca5ebf564d35

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page