Skip to content

when working with FastAPI will 'strip off' some of the fields in schema #559

Answered by JackySu
JackySu asked this question in Questions
Discussion options

You must be logged in to vote

this issue can be solved by these modifications
models.py

class CategoryBase(SQLModel):
    categoryId: int = Field(primary_key=True, nullable=False)
    categoryName: str


class Category(CategoryBase, table=True):
    __tablename__ = 'categories'
    tags: List["Tag"] = Relationship(back_populates="category")


class CategoryWithTags(CategoryBase):
    tags: List["Tag"] = []

CategoryWithTags.update_forward_refs()

api.py

@router.get("/tags", response_model=List[CategoryWithTags])
async def get_category_with_tags(session: AsyncSession = Depends(get_session)) -> List[CategoryWithTags]:

    r = await session.execute(select(Category).options(selectinload(Category.tags)).order_by(Category.c…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by JackySu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant