Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Integration to FastAPI schema example can be updated to Pydantic v2 #733

Open
danielMicallef opened this issue Nov 15, 2024 · 0 comments

Comments

@danielMicallef
Copy link

Documentation for Integration with FastAPI example is still using Pydantic v1. FastAPI with Pydantic v2 support was released back in Aug 2023. Since Pydantic v2 introduces a number of API changes as discussed in the Pydantic migration guide, it would be ideal to reflect this in the documentation.

Specifically:

Hence the Schema example can be updated from:

class CarOut(BaseModel):
    make: str
    model: str

class PersonInDB(BaseModel):
    name: str
    age: int
    cars: List[CarOut]

    @validator('cars', pre=True, allow_reuse=True)
    def pony_set_to_list(cls, values):
        return [v.to_dict() for v in values]

    class Config:
        orm_mode = True

to

from pydantic import BaseModel, field_validator


class CarOut(BaseModel):
    make: str
    model: str

class PersonInDB(BaseModel):
    name: str
    age: int
    cars: List[CarOut]

    @field_validator('cars', mode='before')
    @classmethod
    def pony_set_to_list(cls, values: list) -> list:
        return [CarOut.model_validate(v) if not isinstance(v, CarOut) else v for v in values]

    class Config:
        from_attributes = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant