02 自动生成根据ID查询接口
《zdppy_aocrud官方教程》 02 自动生成根据ID查询接口
自动生成根据ID查询用户的方法
服务端
import aocrud
import env
import orm
from api import Api, respenv.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_by_id(User, "user")app = Api(routes=[resp.get(path, method),resp.get("/users/{id}", method),
])if __name__ == "__main__":import uvicornuvicorn.run("server:app", port=8000)
客户端
import reqr = req.get("http://127.0.0.1:8000/user/3")
print(r.json())r = req.get("http://127.0.0.1:8000/users/3")
print(r.json())
自动生成根据ID查询用户的路由
服务端
import aocrud
import env
import orm
from api import Apienv.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_by_id_route(User, "user"),aocrud.route.get_by_id_route(User, "users"),
])if __name__ == "__main__":import uvicornuvicorn.run("server:app", port=8000)
客户端
import reqr = req.get("http://127.0.0.1:8000/user/3")
print(r.json())r = req.get("http://127.0.0.1:8000/users/3")
print(r.json())