【问题标题】:FastApi pagination error using fastapi-contrib使用 fastapi-contrib 的 FastApi 分页错误
【发布时间】:2020-12-08 01:47:44
【问题描述】:

我正在尝试将分页添加到我的 fastapi 项目中。所以我决定使用这个:

fastapi-contrib

我在那里遵循相同的示例,但由于某种原因,我收到了这个错误:

type object 'MOrdenesTrabajo' has no attribute 'count'

这是我的代码,谢谢你的帮助!!

路线

@router.get("/ordenes-trabajo")
async def read_ot(pagination: Pagination = Depends(), db: Session =Depends(get_db)):
   filter_kwargs = {}
  return await pagination.paginate(serializer_class=OtSerializer.MOrdenesTrabajo, **filter_kwargs)

Pydantic 模型

class MOrdenesTrabajo(ModelSerializer):

   class Meta:
    model = OT # <---Model below

Sqlalchemy 模型

class MOrdenesTrabajo(Base):
__tablename__ = 'M_ordenesTrabajos'

idordenesTrabajos = Column(Integer, primary_key=True)
fecCreacion = Column(Date)
fecModificacion = Column(Date)
estado = Column(Integer)
descripcion = Column(Text)
nombre = Column(String(100))
documento = Column(Text)
fechaOrden = Column(Date)
numero = Column(Text)
ot_padre = Column(Integer)
data = Column(JSON)

【问题讨论】:

    标签: python rest sqlalchemy pagination fastapi


    【解决方案1】:

    我认为你应该使用sqlalchemy-filters。我在我的 FastAPI 项目中使用它进行分页,它按预期工作。 我的分页代码如下所示:

    # pagination
    num_of_pages = 0
    total_results = 0
    if skip and limit:
        results, pagination = apply_pagination(
             results,
             page_number=skip,
             page_size=limit
        )
        num_of_pages = pagination.num_pages
        total_results = pagination.total_results
    

    【讨论】:

      【解决方案2】:

      您没有尝试过 FastAPI_pagination 库吗? 我建议你访问这个链接“https://uriyyo-fastapi-pagination.netlify.app/”这是我在我的代码中使用的方法,它工作得很好

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2020-05-25
      • 2022-07-20
      • 2021-08-31
      • 1970-01-01
      • 2022-07-14
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多