File: README.md

package info (click to toggle)
python-marshmallow-enum 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 140 kB
  • sloc: python: 359; sh: 8; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# marshmallow-enum

Enum field for use with Marshmallow.

## Installation

```bash
pip install --user marshmallow_enum
```

If you're on a version before 3.4, you'll also need to install `enum34`.

## Using The Field

To make use of the field, you must have an existing Enum:

```python
from enum import Enum


class StopLight(Enum):
    green = 1
    yellow = 2
    red = 3
```

Then, declare it as a field in a schema:

```python
from marshmallow import Schema
from marshmallow_enum import EnumField


class TrafficStop(Schema):
    light_color = EnumField(StopLight)
```

By default, the field will load and dump based on the _name_ given to an enum value.

```python
schema = TrafficStop()
schema.dump({'light_color': EnumField.red}).data
# {'light_color': 'red'}

schema.load({'light_color': 'red'}).data
# {'light_color': StopLight.red}
```

### Customizing loading and dumping behavior

To customize how an enum is serialized or deserialized, there are three options:

-   Setting `by_value=True`. This will cause both dumping and loading to use the value of the enum.
-   Setting `load_by=EnumField.VALUE`. This will cause loading to use the value of the enum.
-   Setting `dump_by=EnumField.VALUE`. This will cause dumping to use the value of the enum.

If either `load_by` or `dump_by` are unset, they will follow from `by_value`.

Additionally, there is `EnumField.NAME` to be explicit about the load and dump behavior, this
is the same as leaving both `by_value` and either `load_by` and/or `dump_by` unset.

### Custom Error Message

A custom error message can be provided via the `error` keyword argument. It can accept three
format values:

-   `{input}`: The value provided to the schema field
-   `{names}`: The names of the individual enum members
-   `{values}`: The values of the individual enum members

Previously, the following inputs were also available but are deprecated and will be removed in 1.6:

-   `{name}`
-   `{value}`
-   `{choices}`