[FastAPI-14]pydantic多个请求体

发布时间 2023-03-23 23:52:10作者: LeoShi2020
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

'''
多个请求体
{
  "user": {
    "username": "string",
    "password": "string"
  },
  "item": {
    "name": "string",
    "description": "string",
    "price": 0,
    "tax": 0
  }
}
'''

class User(BaseModel):
    username: str
    password: str


class Item(BaseModel):
    name: str
    description: str
    price: float
    tax: float


@app.post("/login")
def login(user: User, item: Item):
    return {"user":user,
            "item":item}