Pydantic Collections

0.6.0 · active · verified Mon Apr 13

Pydantic-collections provides the `BaseCollectionModel` class, which allows for the manipulation and validation of collections of Pydantic models. It supports various Pydantic-compatible types within its collections. The library is actively maintained, with releases often corresponding to updates in the Pydantic ecosystem, and the current version is 0.6.0.

Warnings

Install

Imports

Quickstart

This example demonstrates defining a Pydantic model (`User`) and then using `BaseCollectionModel` to create a validated collection of `User` objects. It shows instantiation and serialization for Pydantic V2.

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)
# For Pydantic v2.x, use model_dump() and model_dump_json()
print(users.model_dump())
print(users.model_dump_json())

view raw JSON →