Skip to main content

A pydantic model subclass that implements Python's dictionary interface.

Project description

pydantic_dictionary

A pydantic model subclass that implements Python's dictionary interface.

Example:

from pydantic_dict import BaseModelDict

class User(BaseModelDict):
    id: int
    name: str = "Jane Doe"

user = User(id=42)

user["session_id"] = "95607c42-250a-4913-9dfb-00eb6535e685"
assert user.session_id == "95607c42-250a-4913-9dfb-00eb6535e685"
assert user["session_id"] == "95607c42-250a-4913-9dfb-00eb6535e685"
user.pop("session_id")
assert "session_id" not in user

assert user.get("last_name", None) is None

user.update({"email": "jane.doe@email.com"})
print(user.json())
# >>> {"id": 42, "name": "Jane Doe", "email": "jane.doe@email.com"}

user.clear()  # field's are NOT removed. only non-field's are removed
print(user.json())
# >>> {"id": 42, "name": "Jane Doe"}

user.setdefault("last_active", "2023-01-01T19:56:10Z")
del user["last_active"]

Install

pip install pydantic_dict

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_dict-0.0.1.tar.gz (9.3 kB view hashes)

Uploaded Source

Built Distribution

pydantic_dict-0.0.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page