Collections of pydantic models
Project description
pydantic-collections
The pydantic-collections
package provides BaseCollectionModel
class that allows you
to manipulate collections of pydantic models
(and any other types supported by pydantic).
Requirements
- Python>=3.7
- pydantic>=1.8.2,<2.0
Installation
pip install pydantic-collections
Usage
Basic usage
from datetime import datetime
from pydantic import BaseModel
from pydantic_collections import BaseCollectionModel
class User(BaseModel):
id: int
name: str
birth_date: datetime
class UserCollection(BaseCollectionModel[User]):
pass
user_data = [
{'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'},
{'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'},
]
users = UserCollection(user_data)
print(users)
#> UserCollection([User(id=1, name='Bender', birth_date=datetime.datetime(2010, 4, 1, 12, 59, 59)), User(id=2, name='Balaganov', birth_date=datetime.datetime(2020, 4, 1, 12, 59, 59))])
print(users.dict())
#> [{'id': 1, 'name': 'Bender', 'birth_date': datetime.datetime(2010, 4, 1, 12, 59, 59)}, {'id': 2, 'name': 'Balaganov', 'birth_date': datetime.datetime(2020, 4, 1, 12, 59, 59)}]
print(users.json())
#> [{"id": 1, "name": "Bender", "birth_date": "2010-04-01T12:59:59"}, {"id": 2, "name": "Balaganov", "birth_date": "2020-04-01T12:59:59"}]
Strict assignment validation
By default BaseCollectionModel
has a strict assignment check
...
users = UserCollection()
users.append(User(id=1, name='Bender', birth_date=datetime.utcnow())) # OK
users.append({'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'})
#> pydantic.error_wrappers.ValidationError: 1 validation error for UserCollection
#> __root__ -> 2
#> instance of User expected (type=type_error.arbitrary_type; expected_arbitrary_type=User)
This behavior can be changed via Model Config
...
class UserCollection(BaseCollectionModel[User]):
class Config:
validate_assignment_strict = False
users = UserCollection()
users.append({'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'}) # OK
assert users[0].__class__ is User
assert users[0].id == 1
Using as a model field
BaseCollectionModel
is a subclass of BaseModel
, so you can use it as a model field
...
class UserContainer(BaseModel):
users: UserCollection = []
data = {
'users': [
{'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'},
{'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'},
]
}
container = UserContainer(**data)
container.users.append(User(...))
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for pydantic-collections-0.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9bbfe927c561ec9a15639f0f715ea9d905baebfe84132caece5aacb8203eb7d |
|
MD5 | f74cb55eecd5354fd8e13429dc1cb2a5 |
|
BLAKE2b-256 | 3a104beb18bb6c82194d9985b874aac32b537b21c8a661995208f834f7f53b22 |
Close
Hashes for pydantic_collections-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73d7ec8f3b7cc65e201785b343dab04d256c4cbdc5801fd76f59870d32e9a0e0 |
|
MD5 | 95007e6595c5007afb493ff0658937d0 |
|
BLAKE2b-256 | 3986ec8caa1f6d70767ac6b329ddf9f524db5d1c7e333a0be7baf55a14bf2854 |