《zdppy_aocrud官方教程》 11 自动生成分页查询接口

11 自动生成分页查询接口

《zdppy_aocrud官方教程》 11 自动生成分页查询接口

自动生成根据ID分页查询的方法

服务端

import aocrud
import env
import orm
from api import Api, resp

env.load("../.env")

db, BaseModel = orm.get_mysql_pool_base_model_by_env()


class User(BaseModel):
    name = orm.CharField()
    age = orm.IntegerField()


path, method = aocrud.method.get_page(User, "user")
app = Api(routes=[
    resp.get(path, method),
])

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("server:app", port=8000)

客户端

import req

# 默认:page=1 size=20 order=-id
r = req.get("http://127.0.0.1:8000/user_page")
print(r.json())

# page=2 size=2 order=id desc=false
r = req.get("http://127.0.0.1:8000/user_page?page=2&size=2&order=id&desc=false")
print(r.json())

# page=2 size=2 order=age desc=false
r = req.get("http://127.0.0.1:8000/user_page?page=2&size=2&order=age&desc=false")
print(r.json())

自动生成根据ID分页查询的路由

服务端

import aocrud
import env
import orm
from api import Api

env.load("../.env")

db, BaseModel = orm.get_mysql_pool_base_model_by_env()


class User(BaseModel):
    name = orm.CharField()
    age = orm.IntegerField()


app = Api(routes=[
    aocrud.route.get_page_route(User, "user"),
])

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("server:app", port=8000)

客户端

import req

# 默认:page=1 size=20 order=-id
r = req.get("http://127.0.0.1:8000/user_page")
print(r.json())

# page=2 size=2 order=id desc=false
r = req.get("http://127.0.0.1:8000/user_page?page=2&size=2&order=id&desc=false")
print(r.json())

# page=2 size=2 order=age desc=false
r = req.get("http://127.0.0.1:8000/user_page?page=2&size=2&order=age&desc=false")
print(r.json())

你可能感兴趣的:(python)