简体   繁体   中英

Pydantic - create models recursively?

I'm trying to use Pydantic as follows:

from pydantic import BaseModel


class A(BaseModel):
  prop1: str
  prop2: str


class B(BaseModel):
  a: A


data = {
  'prop1': 'some value',
  'prop2': 'some other value'
}


b = B(**data)

Which gives me the following error:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    b = B(**data)
  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for B
a
  field required (type=value_error.missing)

Is it possible for pydantic to create the A instance B requires?

您可以通过以下方式完成此操作: B(a=data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM