{"id":21215299,"url":"https://github.com/romis2012/pydantic-collections","last_synced_at":"2025-09-19T15:32:20.162Z","repository":{"id":40678389,"uuid":"420416732","full_name":"romis2012/pydantic-collections","owner":"romis2012","description":"Collections of pydantic models","archived":false,"fork":false,"pushed_at":"2024-07-09T10:41:41.000Z","size":51,"stargazers_count":47,"open_issues_count":2,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-05T06:13:43.573Z","etag":null,"topics":["models","parsing","pydantic","python","serialization","validation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/romis2012.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-23T13:13:19.000Z","updated_at":"2025-01-03T19:18:46.000Z","dependencies_parsed_at":"2024-11-20T21:51:09.620Z","dependency_job_id":null,"html_url":"https://github.com/romis2012/pydantic-collections","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"4b9ba9e96fd66d32b21bf8cc88991ddec2e59e52"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romis2012%2Fpydantic-collections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romis2012%2Fpydantic-collections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romis2012%2Fpydantic-collections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romis2012%2Fpydantic-collections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/romis2012","download_url":"https://codeload.github.com/romis2012/pydantic-collections/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233574881,"owners_count":18696523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["models","parsing","pydantic","python","serialization","validation"],"created_at":"2024-11-20T21:36:43.138Z","updated_at":"2025-09-19T15:32:14.851Z","avatar_url":"https://github.com/romis2012.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pydantic-collections\n\n[![CI](https://github.com/romis2012/pydantic-collections/actions/workflows/ci.yml/badge.svg)](https://github.com/romis2012/pydantic-collections/actions/workflows/ci.yml)\n[![Coverage Status](https://codecov.io/gh/romis2012/pydantic-collections/branch/master/graph/badge.svg)](https://codecov.io/gh/romis2012/pydantic-collections)\n[![PyPI version](https://badge.fury.io/py/pydantic-collections.svg)](https://pypi.python.org/pypi/pydantic-collections)\n\nThe `pydantic-collections` package provides `BaseCollectionModel` class that allows you \nto manipulate collections of [pydantic](https://github.com/samuelcolvin/pydantic) models \n(and any other types supported by pydantic).\n\n\n## Requirements\n- Python\u003e=3.7\n- pydantic\u003e=1.8.2,\u003c3.0\n\n\n## Installation\n\n```\npip install pydantic-collections\n```\n\n## Usage\n\n#### Basic usage\n```python\n\nfrom datetime import datetime\n\nfrom pydantic import BaseModel\nfrom pydantic_collections import BaseCollectionModel\n\n\nclass User(BaseModel):\n    id: int\n    name: str\n    birth_date: datetime\n\n\nclass UserCollection(BaseCollectionModel[User]):\n    pass\n\n\n user_data = [\n        {'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'},\n        {'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'},\n    ]\n\nusers = UserCollection(user_data)\n\nprint(users)\n#\u003e 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))])\n\nprint(users.dict())  # pydantic v1.x\nprint(users.model_dump())  # pydantic v2.x\n#\u003e [{'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)}]\n\nprint(users.json()) # pydantic v1.x\nprint(users.model_dump_json()) # pydantic v2.x\n#\u003e [{\"id\": 1, \"name\": \"Bender\", \"birth_date\": \"2010-04-01T12:59:59\"}, {\"id\": 2, \"name\": \"Balaganov\", \"birth_date\": \"2020-04-01T12:59:59\"}]\n```\n\n#### Strict assignment validation\n\nBy default `BaseCollectionModel` has a strict assignment check\n```python\n...\nusers = UserCollection()\nusers.append(User(id=1, name='Bender', birth_date=datetime.utcnow()))  # OK\nusers.append({'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'})\n#\u003e pydantic.error_wrappers.ValidationError: 1 validation error for UserCollection\n#\u003e __root__ -\u003e 2\n#\u003e  instance of User expected (type=type_error.arbitrary_type; expected_arbitrary_type=User)\n```\n\nThis behavior can be changed via Model Config\n\nPydantic v1.x\n```python\nfrom pydantic_collections import BaseCollectionModel\n...\nclass UserCollection(BaseCollectionModel[User]):\n    class Config:\n        validate_assignment_strict = False\n```\n\nPydantic v2.x\n```python\nfrom pydantic_collections import BaseCollectionModel, CollectionModelConfig\n...\nclass UserCollection(BaseCollectionModel[User]):\n    model_config = CollectionModelConfig(validate_assignment_strict=False)\n```\n\n```python\nusers = UserCollection()\nusers.append({'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'})  # OK\nassert users[0].__class__ is User\nassert users[0].id == 1\n```\n\n#### Using as a model field\n\n`BaseCollectionModel` is a subclass of `BaseModel`, so you can use it as a model field\n```python\n...\nclass UserContainer(BaseModel):\n    users: UserCollection = []\n        \ndata = {\n    'users': [\n        {'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'},\n        {'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'},\n    ]\n}\n\ncontainer = UserContainer(**data)\ncontainer.users.append(User(...))\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromis2012%2Fpydantic-collections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fromis2012%2Fpydantic-collections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromis2012%2Fpydantic-collections/lists"}