FastAPI 封装分页

  • 分页封装

from fastapi import Depends, FastAPI

app = FastAPI()


fake_items_db = [{"city": "beijing"}, {"city": "shanghai"},
                 {"city": "heze"}]


class CommonQueryParams:
    def __init__(self, desc: str , skip: int = 0, limit: int = 100):
        self.desc = desc
        self.skip = skip
        self.limit = limit

@app.get("/items/")
def read_items(commons: CommonQueryParams = Depends()):
    response = {}
    if commons.desc:
        response.update({"desc": commons.desc})
    items = fake_items_db[commons.skip : commons.skip + commons.limit]
    response.update({"items": items})
    return response

Last updated