Skip to main content

Peewee ORM 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.DefaultConverter

            # 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):
  # ...

Customize fields convertion:

from marshmallow_peewee import DefaultConverter

# Customize global

# Serialize boolean as string
DefaultConverter.register(peewee.BooleanField, marshmallow.fields.String)

# Alternative method
@DefaultConverter.register(peewee.BooleanField)
def build_field(field: peewee.Field, opts, **field_params):
  return marshmallow.fields.String(**params)

# Customize only for a scheme

class CustomConverter(DefaultConverter):
  pass


CustomConverter.register(...)


class CustomSchema(ModelSchema): # may be inherited
  class Meta:
    model_converter = CustomConverter

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

Uploaded Source

Built Distribution

marshmallow_peewee-4.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_peewee-4.1.0.tar.gz.

File metadata

  • Download URL: marshmallow_peewee-4.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for marshmallow_peewee-4.1.0.tar.gz
Algorithm Hash digest
SHA256 6a0a80ecaa9ceaf6fa5297c6677da71106a6f3659e91da25a45769c25601813f
MD5 eb3ba587be389d84032c32a63105c681
BLAKE2b-256 e9e39fe4df46ca1e750ffbda7d59407f2a4effbf2827049561c03ac2bdf3ef45

See more details on using hashes here.

File details

Details for the file marshmallow_peewee-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: marshmallow_peewee-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for marshmallow_peewee-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45b6de788dd5ae8c44207d8c495ae859c9ab433f4fa763746d66bed314655dd6
MD5 ea0195085524483ae37b69c39acb588f
BLAKE2b-256 316812404815d53f2838ef4c1f6444a23e5dbde6ac1e870315e4d0ac6e01fa6e

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