Skip to main content

Peewee integration with the marshmallow (de)serialization library.

Project description

Build Status Version License

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

Marshmallow-Peewee should be installed using pip:

pip install Marshmallow-Peewee

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

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 Marshmallow-Peewee happens at: https://github.com/klen/marshmallow-peewee

Contributors

License

Licensed under a MIT license (See LICENSE)

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-2.3.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

Marshmallow_Peewee-2.3.0-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: Marshmallow-Peewee-2.3.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.3

File hashes

Hashes for Marshmallow-Peewee-2.3.0.tar.gz
Algorithm Hash digest
SHA256 13274ee3931a126caf302244297e50e27bcc66e59776400d736428cfad098c3e
MD5 ef8db3ae2daafde58c9c35f348c46fb8
BLAKE2b-256 55de880e245416171d3fcc20cf47562d17ef7ce81bfbf727ec0e268c06264bde

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Marshmallow_Peewee-2.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.3

File hashes

Hashes for Marshmallow_Peewee-2.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 792de6d59c7a46cc9024bb7cfff3e580a84050a8e7a3a6e56d89899d8e11ca5a
MD5 b97ef912b290c538f5d4febaa65a5d52
BLAKE2b-256 1cb48a3b0de899fc8b30d8730f83fbee74113795625a0a88e8c7e807ac41b78f

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