Short way to get all field names of a pydantic class

What about just using __fields__:

from pydantic import BaseModel

class AdaptedModel(BaseModel):
    parent_attr: str

class TestClass(AdaptedModel):
    child_attr: str
        
TestClass.__fields__

Output:

{'parent_attr': ModelField(name="parent_attr", type=str, required=True),
 'child_attr': ModelField(name="child_attr", type=str, required=True)}

This is just a dict and you could get only the field names simply by: TestClass.__fields__.keys()

See model properties: https://pydantic-docs.helpmanual.io/usage/models/#model-properties

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)