Files
pydantic/docs/examples/custom_root_field.py
T
dmontagu 65d838aa88 Serialize only __root__ value (#752)
* Serialize only __root__ value, fix #730

* Add changes and fix docs

* Clean up root model docs

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Rename variable

* fix rst in change description
2019-08-17 18:58:32 +01:00

36 lines
667 B
Python

from typing import List
import json
from pydantic import BaseModel
from pydantic.schema import schema
class Pets(BaseModel):
__root__: List[str]
print(Pets(__root__=['dog', 'cat']))
# > Pets __root__=['dog', 'cat']
print(Pets(__root__=['dog', 'cat']).json())
# ["dog", "cat"]
print(Pets.parse_obj(['dog', 'cat']))
# > Pets __root__=['dog', 'cat']
print(Pets.schema())
# > {'title': 'Pets', 'type': 'array', 'items': {'type': 'string'}}
pets_schema = schema([Pets])
print(json.dumps(pets_schema, indent=2))
"""
{
"definitions": {
"Pets": {
"title": "Pets",
"type": "array",
"items": {
"type": "string"
}
}
}
}
"""