marshmallow-peewee


Namemarshmallow-peewee JSON
Version 4.3.1 PyPI version JSON
download
home_pagehttps://github.com/klen/marshmallow-peewee
SummaryPeewee ORM integration with the Marshmallow (de)serialization library
upload_time2023-06-30 13:22:20
maintainer
docs_urlNone
authorKirill Klenov
requires_python>=3.8,<4.0
licenseMIT
keywords marshmallow peewee orm serialization deserialization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Marshmallow-Peewee

Marshmallow-Peewee -- [Peewee ORM](https://github.com/coleifer/peewee)
integration with the
[Marshmallow](https://github.com/marshmallow-code/marshmallow)
(de)serialization library.

[![Tests Status](https://github.com/klen/marshmallow-peewee/workflows/tests/badge.svg)](https://github.com/klen/marshmallow-peewee/actions)
[![PYPI Version](https://img.shields.io/pypi/v/marshmallow-peewee)](https://pypi.org/project/marshmallow-peewee/)
[![Python Versions](https://img.shields.io/pypi/pyversions/marshmallow-peewee)](https://pypi.org/project/marshmallow-peewee/)


## Requirements

* python >= 3.7

## Installation

**marshmallow-peewee** should be installed using pip:

```shell
$ pip install marshmallow-peewee
```

### Quickstart

```python
    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

```python

    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`:

```python

from marshmallow_peewee import setup

setup(id_keys=True, string_keys=False)  # Set options for all schemas

class UserSchema(ModelSchema):
  # ...

```

Customize fields convertion:

```python

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](http://opensource.org/licenses/MIT)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klen/marshmallow-peewee",
    "name": "marshmallow-peewee",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "marshmallow,peewee,orm,serialization,deserialization",
    "author": "Kirill Klenov",
    "author_email": "horneds@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/30/f3/070110509a56cad78fca9cbba6548670a181b1937139b725f9c0c83e4178/marshmallow_peewee-4.3.1.tar.gz",
    "platform": null,
    "description": "# Marshmallow-Peewee\n\nMarshmallow-Peewee -- [Peewee ORM](https://github.com/coleifer/peewee)\nintegration with the\n[Marshmallow](https://github.com/marshmallow-code/marshmallow)\n(de)serialization library.\n\n[![Tests Status](https://github.com/klen/marshmallow-peewee/workflows/tests/badge.svg)](https://github.com/klen/marshmallow-peewee/actions)\n[![PYPI Version](https://img.shields.io/pypi/v/marshmallow-peewee)](https://pypi.org/project/marshmallow-peewee/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/marshmallow-peewee)](https://pypi.org/project/marshmallow-peewee/)\n\n\n## Requirements\n\n* python >= 3.7\n\n## Installation\n\n**marshmallow-peewee** should be installed using pip:\n\n```shell\n$ pip install marshmallow-peewee\n```\n\n### Quickstart\n\n```python\n    import peewee as pw\n\n\n    class Role(pw.Model):\n        name = pw.CharField(255, default='user')\n\n\n    class User(pw.Model):\n\n        created = pw.DateTimeField(default=dt.datetime.now())\n        name = pw.CharField(255)\n        title = pw.CharField(127, null=True)\n        active = pw.BooleanField(default=True)\n        rating = pw.IntegerField(default=0)\n\n        role = pw.ForeignKeyField(Role)\n\n\n    from marshmallow_peewee import ModelSchema\n\n    class UserSchema(ModelSchema):\n\n        class Meta:\n            model = User\n\n    role = Role.create()\n    user = User.create(name='Mike', role=role)\n\n    result = UserSchema().dump(user)\n    print(result)\n    # {'active': True,\n    #  'created': '2016-03-29T15:27:18.600034+00:00',\n    #  'id': 1,\n    #  'name': 'Mike',\n    #  'rating': 0,\n    #  'role': 1,\n    #  'title': None}\n\n    result = UserSchema().load(result)\n    assert isinstance(result, User)\n    assert result.name == 'Mike'\n\n    from marshmallow_peewee import Related\n\n    class UserSchema(ModelSchema):\n\n        role = Related()\n\n        class Meta:\n            model = User\n\n    result = UserSchema().dump(user)\n    print(result)\n    # {'active': True,\n    #  'created': '2016-03-29T15:30:32.767483+00:00',\n    #  'id': 1,\n    #  'name': 'Mike',\n    #  'rating': 0,\n    #  'role': {'id': 5, 'name': 'user'},\n    #  'title': None}\n\n    result = UserSchema().load(result)\n    assert isinstance(result, User)\n    assert isinstance(result.role, Role)\n```\n\n## Usage\n\n```python\n\n    import peewee as pw\n\n\n    class Role(pw.Model):\n        name = pw.CharField(255, default='user')\n\n\n    class User(pw.Model):\n\n        created = pw.DateTimeField(default=dt.datetime.now())\n        name = pw.CharField(255)\n        title = pw.CharField(127, null=True)\n        active = pw.BooleanField(default=True)\n        rating = pw.IntegerField(default=0)\n\n        role = pw.ForeignKeyField(Role)\n\n\n    from marshmallow_peewee import ModelSchema\n\n    class UserSchema(ModelSchema):\n\n        class Meta:\n\n            # model: Bind peewee.Model to the Schema\n            model = User\n\n            # model_converter: Use custom model_converter\n            # model_converter = marshmallow_peewee.DefaultConverter\n\n            # dump_only_pk: Primary key is dump only\n            # dump_only_pk = True\n\n            # string_keys: Convert keys to strings\n            # string_keys = True\n\n            # id_keys: serialize (and deserialize) ForeignKey fields with _id suffix\n            # id_keys = False\n```\n\nYou may set global options for `marshmallow-peewee`:\n\n```python\n\nfrom marshmallow_peewee import setup\n\nsetup(id_keys=True, string_keys=False)  # Set options for all schemas\n\nclass UserSchema(ModelSchema):\n  # ...\n\n```\n\nCustomize fields convertion:\n\n```python\n\nfrom marshmallow_peewee import DefaultConverter\n\n# Customize global\n\n# Serialize boolean as string\nDefaultConverter.register(peewee.BooleanField, marshmallow.fields.String)\n\n# Alternative method\n@DefaultConverter.register(peewee.BooleanField)\ndef build_field(field: peewee.Field, opts, **field_params):\n  return marshmallow.fields.String(**params)\n\n# Customize only for a scheme\n\nclass CustomConverter(DefaultConverter):\n  pass\n\n\nCustomConverter.register(...)\n\n\nclass CustomSchema(ModelSchema): # may be inherited\n  class Meta:\n    model_converter = CustomConverter\n\n\n````\n\n## Bug tracker\n\nIf you have any suggestions, bug reports or annoyances please report them to\nthe issue tracker at https://github.com/klen/marshmallow-peewee/issues\n\n\n## Contributing\n\nDevelopment of the project happens at: https://github.com/klen/marshmallow-peewee\n\n\n## License\n\nLicensed under a [MIT License](http://opensource.org/licenses/MIT)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Peewee ORM integration with the Marshmallow (de)serialization library",
    "version": "4.3.1",
    "project_urls": {
        "Homepage": "https://github.com/klen/marshmallow-peewee",
        "Repository": "https://github.com/klen/marshmallow-peewee"
    },
    "split_keywords": [
        "marshmallow",
        "peewee",
        "orm",
        "serialization",
        "deserialization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38353d523ae5022a1c91c80d9c201fe4bf162b736171f7e194ed2c1e342a9c0b",
                "md5": "58061a45601cc7ea36f42bd2530c9e82",
                "sha256": "c43e19876451549b36f24709284ac1d4f886ba1a934723e2ef0c114ca70c0dd2"
            },
            "downloads": -1,
            "filename": "marshmallow_peewee-4.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "58061a45601cc7ea36f42bd2530c9e82",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 8216,
            "upload_time": "2023-06-30T13:22:18",
            "upload_time_iso_8601": "2023-06-30T13:22:18.296156Z",
            "url": "https://files.pythonhosted.org/packages/38/35/3d523ae5022a1c91c80d9c201fe4bf162b736171f7e194ed2c1e342a9c0b/marshmallow_peewee-4.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "30f3070110509a56cad78fca9cbba6548670a181b1937139b725f9c0c83e4178",
                "md5": "07a1141cc104874427b6f136caa330a4",
                "sha256": "1ba759ead70344264c96609d44c8af94397a2fd095fa82352e2dabe72ac2beda"
            },
            "downloads": -1,
            "filename": "marshmallow_peewee-4.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "07a1141cc104874427b6f136caa330a4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 7352,
            "upload_time": "2023-06-30T13:22:20",
            "upload_time_iso_8601": "2023-06-30T13:22:20.390019Z",
            "url": "https://files.pythonhosted.org/packages/30/f3/070110509a56cad78fca9cbba6548670a181b1937139b725f9c0c83e4178/marshmallow_peewee-4.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-30 13:22:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "klen",
    "github_project": "marshmallow-peewee",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "marshmallow-peewee"
}
        
Elapsed time: 0.09477s