我正在使用FastAPI构建数据API。我希望客户端发布2个24个浮动的列表,稍后我将保存到数据库中。
当我尝试创建Pydantic模型时:
from pydantic import BaseModel
class Prices(BaseModel):
buying_price: list(float)=[]
selling_price: list(float)=[]
我得到以下错误:
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 3, in <module>
class Prices(BaseModel):
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 4, in Prices
buying_price: list(float)=[]
TypeError: 'type' object is not iterable
尽管这个错误是不言自明的,但我不明白
然后,查看文档,我发现了以下方法:
from pydantic import BaseModel
from typing import List
class Prices(BaseModel):
buying_price: List(float)=[]
selling_price: List(float)=[]
但我犯了以下错误。
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 4, in <module>
class Prices(BaseModel):
File "c:/Users/Amin y Lubna/FastAPI-InfluxDB/test.py", line 5, in Prices
buying_price: List(float)=[]
File "C:UsersAmin y Lubnaanaconda3libtyping.py", line 727, in __call__
raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead
我已经和这个错误斗争了几天,我找不到解决问题的办法。
您需要使用list[float]
,而不是list(float)