Skip to main content
Marshmallow Models
==================


Installation
------------

```
pip install marshmallow-models
```


Contributing
------------

Feature requests, feedback,
[issues](https://github.com/douglas-treadwell/marshmallow-models/issues),
and pull requests are welcome and appreciated.

Please follow the [Marshmallow Contributing Guidelines](
https://github.com/marshmallow-code/marshmallow/blob/dev/CONTRIBUTING.rst
).


Overview
--------

Inspired by [Schematics](https://github.com/schematics/schematics),
powered by [Marshmallow](https://github.com/marshmallow-code/marshmallow).

Whereas Marshmallow is an excellent serialization/deserialization
and validation library, it wasn't intended to be a class or type
definition library, which Schematics was.

This library provides a Schematics-like Model but
using Marshmallow's Fields and validation. This library also
intentionally maintains the usage style of marshmallow so that
users of Marshmallow Schemas will be able to use these Models easily.

Usage
-----

Models are defined like Schemas, but whereas a Schema is instantiated
with parameters and then used to schema.dump(data) or schema.load(data),
or schema.validate(data),
Models are instantiated, attributes may be assigned to them, and then
they can be .dump()'d, .dumps()'d or .validate()'d.

### Basic Usage

```python
from marshmallow_models import Model
from marshmallow.fields import String, Integer

class PersonModel(Model):
name = String(required=True)
age = Integer(required=True)

person = PersonModel()

person.name = 'Tester'
person.age = 100

# or equivalently:
person = PersonModel({'name': 'Tester', 'age': 100})

# or equivalently:
person = PersonModel(name='Tester', age=100)

# throws marshmallow.exceptions.ValidationError if invalid
person.validate()

person.dump().data # {'name': 'Tester', 'age': 100}
```

### Missing and Default Attributes

```python
class PersonModel(Model):
name = String(missing='Anonymous')
age = Integer(default=0)

person = PersonModel()

person.name # 'Anonymous'
person.age # 0
```

Default and missing parameters may be provided as they are to
Marshmallow Schemas.

Constructing a model is treated like
"loading" data (as in, schema.load(data)). If attributes are
missing and a `missing` configuration was provided, those values
will be assigned to the missing attributes.

Reading attributes is treated like "dumping" data (as in,
schema.dump(data)), as are calls to model.dump() and dumps().
If a value doesn't exist when read or dumped, the default value
will be substituted for that attribute.

In many cases `default` and `missing` can be used interchangeably
in the context of Models, but there may be cases where their
different treatment is meaningful.

### Nested Models

Nested models are also supported.

```python
class ParentModel(PersonModel):
child = NestedModel(PersonModel)

parent = ParentModel(name='Tester', age=40, child=dict(name='Child', age=10))

self.assertEqual(parent.child.name, 'Child')

parent.child.name = 'Kid'

self.assertEqual(parent.child.name, 'Kid')
```

Configuration
-------------

Marshmallow Models support the "class Meta" configuration method.

An additional Meta attribute is supported: `strict_constructor`.

In Marshmallow Schemas, transformation of input data to output data
was a single step process. In Marshmallow Models, it might be
reasonable for users to instantiate a model with incomplete attributes
and then fill in the attributes before attempting to validate() or
dump() the data.

By default, even for Models with `strict = True`
the constructor does not raise exceptions for incomplete attributes.
If exceptions are wanted in this case, set `strict_constructor = True`.


```python
class PersonWithStrictConstructorModel(Model):
class Meta:
strict_constructor = True

name = String(required=True)
age = Integer(required=True)

with self.assertRaises(ValidationError):
person = PersonWithStrictConstructorModel()
```


Metadata

Release files for marshmallow-models 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marshmallow-models 0.2.0
File Size Uploaded Provenance Actions
marshmallow-models-0.2.0.tar.gz 6.2 kB Without provenance Details for marshmallow-models-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for marshmallow-models 0.2.0
File Interpreter ABI Platform Actions
marshmallow_models-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for marshmallow_models-0.2.0-py2.py3-none-any.whl

Total release size: 15.1 kB

Release files / marshmallow-models-0.2.0.tar.gz

Download URL marshmallow-models-0.2.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1b2af1c39f67e676cc382b178e31af5fb83db089446b3c45586286c3b9b2c91f
BLAKE2b-256 checksum
How to use checksums
e969b2a2c5deda2d0682d369b2d7854e55f838a03e6e7da39d115575eef2dd0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / marshmallow_models-0.2.0-py2.py3-none-any.whl

Download URL marshmallow_models-0.2.0-py2.py3-none-any.whl
Size 8.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
40b275036a26c2fad4bd954a3d8089cb1b9cf6c66bc32c0b8653d1fe44766dad
BLAKE2b-256 checksum
How to use checksums
9b8d085f84d069bd25cd41fdc1a13e084faa65c6d3cdfc05f7d1f8e3bfdae0c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for marshmallow-models 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page