fastapi-pagination icon indicating copy to clipboard operation
fastapi-pagination copied to clipboard

I'm facing an issue while customizing `params` and `page` based on the examples in the documentation.

Open pattonLees opened this issue 1 year ago β€’ 3 comments

custom page

class JSONAPIPage(AbstractPage[T], Generic[T]):
    data: Sequence[T]
    meta: JSONAPIPageMeta

    __params_type__ = JSONAPIParams

    @classmethod
    def create(
        cls,
        items: Sequence[T],
        params: AbstractParams,
        *,
        total: Optional[int] = None,
        **kwargs: Any,
    ) -> Self:
        assert isinstance(params, JSONAPIParams)
        assert total is not None

        return cls(
            data=items,
            meta={"page": {"total": total}},
            **kwargs,
        )

custom params

class JSONAPIParams(BaseModel, AbstractParams):
    offset: int = Query(1, ge=1)
    limit: int = Query(10, ge=1, le=100)

    def to_raw_params(self) -> RawParams:
        return RawParams(limit=self.limit, offset=self.offset)

schema

class Goods(BaseModel):
    assert_name: str
    assert_num: str = None
    desc: Optional[str] = None
    unit: str = None
    price: str = None

    class Config:
        from_orm = True

error info

File "/Users/patton/miniconda3/envs/assert_manage_backend/lib/python3.11/site-packages/fastapi_pagination/api.py", line 180, in create_page
   return _page_val.get().create(items, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/Users/patton/PycharmProjects/assert_manage_backend/schemas/bbase_response.py", line 95, in create
   return cls(
          ^^^^
 File "/Users/patton/miniconda3/envs/assert_manage_backend/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
   __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for JSONAPIPage[Goods]
data.0
 Input should be a valid dictionary or instance of Goods [type=model_type, input_value=Goods(assert_num='6', id=..., desc=None, unit='δΈͺ'), input_type=Goods]
   For further information visit https://errors.pydantic.dev/2.5/v/model_type

Could someone help me identify where the issue is? Thank you for your assistance.

pattonLees avatar Jan 19 '24 11:01 pattonLees

Hi @pattonLees,

Docs seems to be a bit outdated and use pydantic v1 and you are using pydantic v2.

Could you try to update Goods schema?:

class Goods(BaseModel):
    assert_name: str
    assert_num: str = None
    desc: Optional[str] = None
    unit: str = None
    price: str = None
   
   model_config = {
      "from_attributes": True,
   }

uriyyo avatar Jan 19 '24 11:01 uriyyo

@uriyyo Thank you for your guidance, the program is running smoothly now.After adding Pydantic, it seems that troubleshooting becomes more challenging, especially in complex scenarios with intricate type models. Do you have any advice on becoming proficient in using and debugging Pydantic errors?

pattonLees avatar Jan 19 '24 12:01 pattonLees

@pattonLees Practice makes perfect)

I would recommend check out pydantic docs they are really great - https://docs.pydantic.dev/latest/

uriyyo avatar Jan 19 '24 12:01 uriyyo

@pattonLees Practice makes perfect)

I would recommend check out pydantic docs they are really great - https://docs.pydantic.dev/latest/

Thank you for your advice, learning from you to become an expert.:)

pattonLees avatar Jan 20 '24 13:01 pattonLees