在没有 Pydantic BaseModel 的情况下获取类的属性

时间:2021-07-22 14:29:27

标签: python class attributes pydantic

这个解决方案似乎有助于解决我的问题:Getting attributes of a class

但是,当我按照上面链接的步骤进行操作时,我的项目仅返回 Configfields。这可能是因为这些类继承自 Pydantic 的 BaseModel

我要找的是一个班级,比如说:

class GameStatistics(BaseModel):
    id: UUID
    status: str
    scheduled: datetime

我应该返回 idstatusscheduled

我的尝试:

    for name, cls in inspect.getmembers(module, lambda member: inspect.isclass(member) and member.__module__== module.__name__):

        for _name, attrbs in inspect.getmembers(cls, lambda member: not(inspect.isroutine(member))):
            if not _name.startswith('_'):
                print(_name, attrbs)

0 个答案:

没有答案