marshmallow-peewee


Namemarshmallow-peewee JSON
Version 5.0.0 PyPI version JSON
download
home_pagehttps://github.com/klen/marshmallow-peewee
SummaryPeewee ORM integration with the Marshmallow (de)serialization library
upload_time2024-11-21 10:39:35
maintainerNone
docs_urlNone
authorKirill Klenov
requires_python<4.0,>=3.9
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.9

## 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": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "marshmallow, peewee, orm, serialization, deserialization",
    "author": "Kirill Klenov",
    "author_email": "horneds@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f1/f1/9746e1a8d5e823d04ca4325f54113df21d65f33af3227093568c648d020d/marshmallow_peewee-5.0.0.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.9\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": "5.0.0",
    "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": "b6c8d7100a3fec923c2d8da96f7fc1fe5030628e19c37b3289dddc9540fac315",
                "md5": "b3617fdaecc95e98bf0ba6596781f8d5",
                "sha256": "f64f6032b6647fd90786757cdff9c9c17ce8e49c8af7e6d0c5749e3741086e8b"
            },
            "downloads": -1,
            "filename": "marshmallow_peewee-5.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b3617fdaecc95e98bf0ba6596781f8d5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 8251,
            "upload_time": "2024-11-21T10:39:33",
            "upload_time_iso_8601": "2024-11-21T10:39:33.790827Z",
            "url": "https://files.pythonhosted.org/packages/b6/c8/d7100a3fec923c2d8da96f7fc1fe5030628e19c37b3289dddc9540fac315/marshmallow_peewee-5.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f1f19746e1a8d5e823d04ca4325f54113df21d65f33af3227093568c648d020d",
                "md5": "004d0795c835ac46a31c01fb2c430cad",
                "sha256": "c963f3b2914490354055979e25b9a92c87c5a1f72c5fee785667b3e1c336a817"
            },
            "downloads": -1,
            "filename": "marshmallow_peewee-5.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "004d0795c835ac46a31c01fb2c430cad",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 7395,
            "upload_time": "2024-11-21T10:39:35",
            "upload_time_iso_8601": "2024-11-21T10:39:35.198789Z",
            "url": "https://files.pythonhosted.org/packages/f1/f1/9746e1a8d5e823d04ca4325f54113df21d65f33af3227093568c648d020d/marshmallow_peewee-5.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-21 10:39:35",
    "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.40237s