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

issue with related field from tortoise orm pydantic schema #153

Open
minkoonim opened this issue Apr 3, 2022 · 0 comments
Open

issue with related field from tortoise orm pydantic schema #153

minkoonim opened this issue Apr 3, 2022 · 0 comments

Comments

@minkoonim
Copy link

I'm using fastapi + tortoise,
problem here is i was keep getting "NoValuesFetched" exception from tortoise
when a model has relation with other models.

so I jumped into TortoiseCRUDRouter class and had a look and added a line of code.
it should be because from_queryset() and from_tortoise_orm() has fetch_related().

it works fine if given schema is tortoise pydantic basemodel.

def _get_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
    async def route(pagination: PAGINATION = self.pagination) -> List[Model]:
        skip, limit = pagination.get("skip"), pagination.get("limit")
        query = self.db_model.all().offset(cast(int, skip))

        if limit:
            query = query.limit(limit)
        # query = await self.schema.from_queryset(query) # added
        return query

    return route

and

def _get_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
    async def route(item_id: int) -> Model:
        model = await self.db_model.filter(id=item_id).first()
        # model = await self.schema.from_tortoise_orm(model) # added
        if model:
            return model
        else:
            raise NOT_FOUND

    return route
Fa2y added a commit to Fa2y/fastapi-crudrouter that referenced this issue Feb 22, 2023
Fa2y added a commit to Fa2y/fastapi-crudrouter that referenced this issue Feb 22, 2023
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