快速声明api、python的对象

Declare fast api, python's object

我们可以声明一个继承 basemodel 的模型并在其中放置一个值,而不是普通的 Python 模型吗?

我可以强制创建 PyTimeZoneSetting 模型,但我可以使用现有的 TimeZoneSetting 吗?

    from pydantic import BaseModel
    from typing import List, Optional

    class TimeZoneSetting(BaseModel):
        time_zone: str
        type: int
        sync_time: Optional[str] = None
        time_server: Optional[str] = None
        time_set_type: Optional[int] = None

    class PyTimeZoneSetting():
        time_zone: str
        type: int
        sync_time: str
        time_server: str
        time_set_type: str

    def update_system_timezone():
        !Here, I want to create a TimeZoneSetting model and put in a value.!

我不太确定您的意图,但我会尝试提供一个可以满足您需求的示例。

这里有一些初始化 Pydantic 模型的方法:

# initialize every field one by one
model_instance = TimeZoneSetting(time_zone="foo", type=1, sync_time="bar")

# provide your dict object
kwargs = {"time_zone": "foo", "type": 1}
model_instance = TimeZoneSetting(**kwargs)

这是更新模型的方法,其中每个字段都作为您的属性到达 class:

model_instance.time_zone = "bar"

这里是让你建模为 dict 的方法:

model_instance.dict()