How to assign label to nested Basemodel in pydantic with fastapi

27 views Asked by At

In my fastapi app I have a pydantic BaseModel 'Invoice', It has a field 'taxes_list', which is just a list of TaxModel. When I read the docs generated I realize that, there is not not indication that, the taxes_list is a list of TaxModel. In the following picture you can see that taxes list is an array of objects and the label of each Item of that list is "Items", instead of "TaxModel". It there any way to substitute the "items" word for the name of the model? Or perhaps give some indication that the taxes_list is a list of TaxModel

Here is my code:

class Invoice(BaseModel):
    taxes_list: Optional[Annotated[list[TaxModel], Field(title="Taxes List",
                                                        description="""TODO Se debe poner una descripcion AKI""")]] = None
                                                        
                                                        
                                                        
class TaxModel(BaseModel):
    """ Tax description"""
    
    base: Decimal = Field(...,
                                title="Base",
                                examples={
                                    1500.00,
                                    300.600000
                                },
                                decimal_places=6,
                                gt=0)
    importe: Decimal = Field(...,
                                title="Importe",
                                examples={
                                    240.00,
                                    0.00
                                },
                                decimal_places=6,
                                ge=0)

    impuesto: ImpuestoEnum = Field(title="Impuesto",
                                        max_length=3,
                                        examples=[
                                            "002",
                                            "001",
                                        ])
    tipo_factor: TipoFactorEnum = Field(title="Tipo Factor",
                                            examples=[
                                                "Tasa",
                                                "Cuota",
                                            ])
    tasa_o_cuota: Decimal = Field(...,
                                    title="Tasa o Cuota",
                                    examples={
                                        0.160000,
                                        0.200000
                                    },
                                    decimal_places=6,
                                    ge=0)

Image of fastapi documentation

0

There are 0 answers