问题
I'm trying to add pagination to my fastapi project. So I decide to use this:
fastapi-contrib
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!!
route
@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 model
class MOrdenesTrabajo(ModelSerializer):
class Meta:
model = OT # <---Model below
Sqlalchemy model
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)
回答1:
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
来源:https://stackoverflow.com/questions/63476011/fastapi-pagination-error-using-fastapi-contrib