【发布时间】:2021-09-17 08:51:43
【问题描述】:
我有以下 pydantic 模型。
class SubModel(BaseModel):
columns: Mapping
key: List[str]
required: Optional[List[str]]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
class MyModel(BaseModel):
name: str
config1: Optional[SubModel]
config2: Optional[Mapping]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
当我尝试对此进行dumps 时,我得到model is not JSON serializable
from io import BytesIO
from orjson import dumps
bucket = s3.Bucket(bucket_name)
bucket.upload(BytesIO(dumps(data)), key, ExtraArgs={'ContentType': 'application/json'})
错误 -
TypeError: Type is not JSON serializable: MyModel
data 是一个普通的 python 字典,其中一个项目类型为MyModel。尝试使用.json(),但得到dict has no attribute json。
我被困在这里。谁能帮帮我。
【问题讨论】:
-
您解决了这个问题吗?