Pydantic - create models recursively?

Viewed 1489

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?

1 Answers

You can accomplish this with the following: B(a=data).

Related