使用 Pydantic 嵌套模型从 FastAPI 获取 JSON
Obtain JSON from FastAPI using Pydantic Nested Models
以下代码接收一些 JSON 已发布到 FastAPI 服务器的内容。 FastAPI 使其在函数中作为 Pydantic 模型可用。我的示例代码通过编写一个文件来处理它。我不喜欢的(这似乎是使用 Pydantic List 的副作用)是我必须返回来获得一些可用的 JSON.
如何在不循环的情况下执行此操作?
我觉得这一定是可能的,因为 return images
很管用。
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
import json
app = FastAPI()
class Image(BaseModel):
url: str
name: str
@app.post("/images/multiple/")
async def create_multiple_images(images: List[Image]):
#return images # returns json string
#print(images) # prints an Image object
#print(images.json()) # AttributeError: 'list' object has no attribute 'json'
#print(json.dumps(images)) # TypeError: Object of type Image is not JSON serializable
img_data = list() # does it really have to be this way?
for i in images:
img_data.append(i.dict())
with open('./images.json', 'w') as f:
json.dump(img_data, f, indent=2)
'''
curl -v -d '[{"name":"wilma","url":"http://this.com"},{"name":"barney","url":"http://that.com"}]' http://localhost:8000/images/multiple/
'''
该示例是从 FastAPI docs
扩展而来的
要在没有循环的情况下转储模型对象列表,pydantic 提供了定义模型的能力 with a custom root type。
这是它的外观的一个小例子:
class Image(BaseModel):
url: str
name: str
class Images(BaseModel):
__root__: List[Image]
images_raw = '[{"url":"url1", "name":"name1"}, {"url":"url2", "name":"name2"}]'
images = parse_raw_as(Images, images_raw)
with open('./images.json', 'w') as f:
f.write(images.json(indent=2))
路径操作的定义如下所示:
@app.post("/images/multiple/")
async def create_multiple_images(images: Images):
with open('./images.json', 'w') as f:
f.write(images.json(indent=2))
以下代码接收一些 JSON 已发布到 FastAPI 服务器的内容。 FastAPI 使其在函数中作为 Pydantic 模型可用。我的示例代码通过编写一个文件来处理它。我不喜欢的(这似乎是使用 Pydantic List 的副作用)是我必须返回来获得一些可用的 JSON.
如何在不循环的情况下执行此操作?
我觉得这一定是可能的,因为 return images
很管用。
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
import json
app = FastAPI()
class Image(BaseModel):
url: str
name: str
@app.post("/images/multiple/")
async def create_multiple_images(images: List[Image]):
#return images # returns json string
#print(images) # prints an Image object
#print(images.json()) # AttributeError: 'list' object has no attribute 'json'
#print(json.dumps(images)) # TypeError: Object of type Image is not JSON serializable
img_data = list() # does it really have to be this way?
for i in images:
img_data.append(i.dict())
with open('./images.json', 'w') as f:
json.dump(img_data, f, indent=2)
'''
curl -v -d '[{"name":"wilma","url":"http://this.com"},{"name":"barney","url":"http://that.com"}]' http://localhost:8000/images/multiple/
'''
该示例是从 FastAPI docs
扩展而来的要在没有循环的情况下转储模型对象列表,pydantic 提供了定义模型的能力 with a custom root type。
这是它的外观的一个小例子:
class Image(BaseModel):
url: str
name: str
class Images(BaseModel):
__root__: List[Image]
images_raw = '[{"url":"url1", "name":"name1"}, {"url":"url2", "name":"name2"}]'
images = parse_raw_as(Images, images_raw)
with open('./images.json', 'w') as f:
f.write(images.json(indent=2))
路径操作的定义如下所示:
@app.post("/images/multiple/")
async def create_multiple_images(images: Images):
with open('./images.json', 'w') as f:
f.write(images.json(indent=2))