Skip to content

Inheritance from base model is erased in post  #546

@vale9003

Description

@vale9003

Example code:

class BaseMessage(BaseModel):
id_trx: float
date: str
id_cost: float
n_id_issuer: float

@classmethod
def get_validators(cls):
    # yield dict_validator
    yield cls.validate

@classmethod
def validate(cls, value):
    if isinstance(value, cls):
        return value
    else:
        return cls(**dict_validator(value))

class SubMessageOne(BaseMessage):
a: str
b: str

class SubMessageTwo(BaseMessage):
c: str

@app.post("/test")
async def test(m: BaseMessage):
return isinstance(m, SubMessageOne)

POST JSON example:

{
"id_trx": 5,
"date": "20190101",
"id_cost": 250.7,
"n_id_issuer": 45,
"a" : "a uno",
"b" : "b uno"

}

It returns false from test method, I cannot access to SubMessageOne attributes a and b.
I would like to know if exits a workaround to solve this issue or it isn't supported.

Best Regards,
Valentina.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions