Skip to main content

Peewee integration with the marshmallow (de)serialization library.

Project description

Marshmallow-Peewee

Marshmallow-Peewee -- Peewee ORM integration with the Marshmallow (de)serialization library.

Tests Status PYPI Version Python Versions

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

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


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

Uploaded Source

Built Distribution

Marshmallow_Peewee-3.1.0-py2.py3-none-any.whl (7.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: Marshmallow-Peewee-3.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for Marshmallow-Peewee-3.1.0.tar.gz
Algorithm Hash digest
SHA256 99e09d00a29cab1c673fdde395a647b08e5e2eeb0937d713fd14681abdeee704
MD5 84eca9b302cfb5700ba60816732da213
BLAKE2b-256 9ed55480aaf2e1a17ea602d60d30a806fb860ef014cb3130c00a7cb5cb36d3c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Marshmallow_Peewee-3.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for Marshmallow_Peewee-3.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1fad9d26291ab45bebc18eda9feea33a1668f5606db1a2c131c352851771f3b9
MD5 ff4a86f7a6eaa78f3f67c59b4cdade0c
BLAKE2b-256 f2d56b79f8e2b55998e2502b29513ff23c6e8e8fa44c48bbc1ce82a10fd13d8b

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