Define a Pydantic (nested) model

We Are Going To Discuss About Define a Pydantic (nested) model. So lets Start this Python Article.

Define a Pydantic (nested) model

  1. How to solve Define a Pydantic (nested) model

    If you don't need data validation that pydantic offers, you can use data classes along with the dataclass-wizard for this same task. It's slightly easier as you don't need to define a mapping for lisp-cased keys such as server-time.
    Simple example below:
    from __future__ import annotations from dataclasses import dataclass from datetime import datetime from dataclass_wizard import fromdict @dataclass class Something: data: Data # or simply: # server_time: str server_time: datetime @dataclass class Data: id: int ks: dict[str, int] items: list[Person] @dataclass class Person: id: int name: str surname: str # note: data is defined in the OP above input_data = ... print(fromdict(Something, input_data))
    Output:
    Something(data=Data(id=81, ks={'k1': 25, 'k2': 5}, items=[Person(id=1, name='John', surname='Smith'), Person(id=2, name='Jane', surname='Doe')]), server_time=datetime.datetime(2021, 12, 9, 14, 18, 40))

  2. Define a Pydantic (nested) model

    If you don't need data validation that pydantic offers, you can use data classes along with the dataclass-wizard for this same task. It's slightly easier as you don't need to define a mapping for lisp-cased keys such as server-time.
    Simple example below:
    from __future__ import annotations from dataclasses import dataclass from datetime import datetime from dataclass_wizard import fromdict @dataclass class Something: data: Data # or simply: # server_time: str server_time: datetime @dataclass class Data: id: int ks: dict[str, int] items: list[Person] @dataclass class Person: id: int name: str surname: str # note: data is defined in the OP above input_data = ... print(fromdict(Something, input_data))
    Output:
    Something(data=Data(id=81, ks={'k1': 25, 'k2': 5}, items=[Person(id=1, name='John', surname='Smith'), Person(id=2, name='Jane', surname='Doe')]), server_time=datetime.datetime(2021, 12, 9, 14, 18, 40))

Solution 1

If you don’t need data validation that pydantic offers, you can use data classes along with the dataclass-wizard for this same task. It’s slightly easier as you don’t need to define a mapping for lisp-cased keys such as server-time.

Simple example below:

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime

from dataclass_wizard import fromdict


@dataclass
class Something:
    data: Data
    # or simply:
    #   server_time: str
    server_time: datetime


@dataclass
class Data:
    id: int
    ks: dict[str, int]
    items: list[Person]


@dataclass
class Person:
    id: int
    name: str
    surname: str


# note: data is defined in the OP above
input_data = ...

print(fromdict(Something, input_data))

Output:

Something(data=Data(id=81, ks={'k1': 25, 'k2': 5}, items=[Person(id=1, name='John', surname='Smith'), Person(id=2, name='Jane', surname='Doe')]), server_time=datetime.datetime(2021, 12, 9, 14, 18, 40))

Original Author rv.kvetch Of This Content

Solution 2

I see that you have taged fastapi and pydantic so i would sugest you follow the official Tutorial to learn how fastapi work. You have a whole part explaining the usage of pydantic with fastapi here.

to respond more precisely to your question pydantic models are well explain in the doc.

simple exemple:

from typing import List
from pydantic import BaseModel

class Data(BaseModel):
    id: int
    ks: str
    items: List[str]

class Something(BaseModel):
    data: Data
    # you can replace it by a pydantic time type that fit your need
    server_time: str = Field(alias="server-time")

Original Author Bastien B Of This Content

Solution 3

from pydantic import BaseModel

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

Original Author Divyanshu Lohar Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment