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,<3.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()) # pydantic v1.x
print(users.model_dump()) # pydantic v2.x
#> [{'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()) # pydantic v1.x
print(users.model_dump_json()) # pydantic v2.x
#> [{"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
Pydantic v1.x
from pydantic_collections import BaseCollectionModel
...
class UserCollection(BaseCollectionModel[User]):
class Config:
validate_assignment_strict = False
Pydantic v2.x
from pydantic_collections import BaseCollectionModel, CollectionModelConfig
...
class UserCollection(BaseCollectionModel[User]):
model_config = CollectionModelConfig(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
File details
Details for the file pydantic_collections-0.6.0.tar.gz
.
File metadata
- Download URL: pydantic_collections-0.6.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c34d3fd1df5600b315cdecdd8e74eacd4c8c607b7e3f2c9392b2a15850a4ef9e |
|
MD5 | 3b371424d881a972f052d0254aa43983 |
|
BLAKE2b-256 | 68b3ca1ba73ce72fc36b950bcc8d499a7085c8709f0a1a17f1e977bcd1ed5e88 |
File details
Details for the file pydantic_collections-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_collections-0.6.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec559722abf6a0f80e6f00b3d28f0f39c0ed5feb1641166230eb75e9da880162 |
|
MD5 | bde5083e6983ea8842042c50af380941 |
|
BLAKE2b-256 | baabf9956f44b42eb6d66b0a542b842cd3f824637b872c549f04a3c9fd6463c3 |