使用fastapi-contrib的FastApi分页错误 [英] FastApi pagination error using fastapi-contrib
本文介绍了使用fastapi-contrib的FastApi分页错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将分页添加到我的fastapi项目中.所以我决定使用这个:
I'm trying to add pagination to my fastapi project. So I decide to use this:
我在那儿遵循相同的示例,但是由于某种原因,我遇到了这个错误:
I follow the same example there, but for some reason I'm getting this error:
type object 'MOrdenesTrabajo' has no attribute 'count'
这是我的代码,谢谢您的帮助!
Here is my code, thanks for your help!!
路线
@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)
热缩模型
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)
推荐答案
我认为您应该使用 sqlalchemy-过滤器.我在FastAPI项目中使用它进行分页,它按预期工作.我的分页代码如下:
I think you should use sqlalchemy-filters. I am using it for pagination in my FastAPI project and it works as expected. My code for pagination looks like this:
# 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
这篇关于使用fastapi-contrib的FastApi分页错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文