0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

fastapi-paginationでSQLModelの単一フィールドを取得するとバリデーションエラーが発生

Posted at

問題

FastAPIにページネーション機能を追加するライブラリ「fastapi-pagination」で以下のようなコードを書いたところ、エラー「 Input should be a valid dictionary or object to extract fields from... 」が発生しました。

from fastapi_pagination.ext.sqlmodel import paginate
from sqlmodel import SQLModel

class User(SQLModel):
    name: str

@router.get("/users", response_model=Page[User])
def get_users(session: SessionDep):
    return paginate(session, select(User.name))
...
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlmodel.py", line 121, in paginate
    return _paginate(
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlalchemy.py", line 283, in paginate
    return exec_pagination(
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlalchemy.py", line 186, in exec_pagination
    return create_page(
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/api.py", line 182, in create_page
    return _page_val.get().create(items, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/default.py", line 73, in create
    return create_pydantic_model(
  File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/utils.py", line 171, in create_pydantic_model
    return model_cls.model_validate(kwargs, from_attributes=True)  # type: ignore
  File "/usr/local/lib/python3.10/site-packages/pydantic/main.py", line 509, in model_validate
    return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 50 validation errors for Page[User]
items.0
  Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value='...', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/model_attributes_type
...

対策

不具合として報告したところ、修正版がリリースされました🚀
同問題が発生した場合は、バージョン 0.12.28 以降にアップデートすると解決します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?