Skip to content

Instantly share code, notes, and snippets.

@sebastianschramm
Created August 13, 2022 18:39
Show Gist options
  • Save sebastianschramm/131aba94066ca32b80db63acd44fa6c8 to your computer and use it in GitHub Desktop.
Save sebastianschramm/131aba94066ca32b80db63acd44fa6c8 to your computer and use it in GitHub Desktop.
Instantiate a pydantic BaseModel from a dictionary object
from datetime import datetime
from pydantic import BaseModel
class Message(BaseModel):
id: int
content: str
created_at: datetime
message_dict = {"id": 1, "content": "Foo bar", "created_at": datetime(2022, 8, 14)}
message = Message.parse_obj(message_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment