《zdppy_aocrud官方教程》 04 自动生成新增接口

04 自动生成新增接口

《zdppy_aocrud官方教程》 04 自动生成新增接口

新增用户

服务端:使用方法

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.add(User, "user")
app = Api(routes=[
    resp.post(path, method),
])

if __name__ == "__main__":
    import uvicorn

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

服务端:使用路由

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.add_route(User, "user"),
])

if __name__ == "__main__":
    import uvicorn

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

服务端:使用默认路径

默认路径是模型名的字符串的蛇形命名格式。比如:

  • User:user
  • UserManager:user_manager
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.add_route(User),
])

if __name__ == "__main__":
    import uvicorn

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

客户端

import req

data = {
    "name": "张三123",
    "age": 23
}
r = req.post("http://127.0.0.1:8000/user", json=data)
print(r.json())

你可能感兴趣的:(python)