Reputation: 3654
I'm trying to implement Pydantic Schema Models for the following JSON.
{
"description": "Best Authors And Their Books",
"authorInfo":
{
"KISHAN":
{
"numberOfBooks": 10,
"bestBookIds": [0, 2, 3, 7]
},
"BALARAM":
{
"numberOfBooks": 15,
"bestBookIds": [10, 12, 14]
},
"RAM":
{
"numberOfBooks": 6,
"bestBookIds": [3,5]
}
}
}
Here are the schema objects in Pydantic
from typing import List, Type, Dict
from pydantic import BaseModel
class AuthorBookDetails(BaseModel):
numberOfBooks: int
bestBookIds: List[int]
class AuthorInfoCreate(BaseModel):
__root__: Dict[str, Type[AuthorBookDetails]]
#pass
class ScreenCreate(BaseModel):
description: str
authorInfo: Type[AuthorInfoCreate]
I'm parsing the AuthorInfoCreate as follows:
y = AuthorBookDetails( numberOfBooks = 10, bestBookIds = [3,5])
print(y)
print(type(y))
x = AuthorInfoCreate.parse_obj({"RAM" : y})
print(x)
I see the following error.
numberOfBooks=10 bestBookIds=[3, 5]
<class '__main__.AuthorBookDetails'>
Traceback (most recent call last):
File "test.py", line 44, in <module>
x = AuthorInfoCreate.parse_obj({"RAM": y})
File "C:\sources\rep-funds\env\lib\site-packages\pydantic\main.py", line 402, in parse_obj
return cls(**obj)
File "C:\sources\rep-funds\env\lib\site-packages\pydantic\main.py", line 283, in __init__
raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for AuthorInfoCreate
__root__ -> RAM
subclass of AuthorBookDetails expected (type=type_error.subclass; expected_class=AuthorBookDetails)
I want to understand how can I change AuthorInfoCreate so that I have the json schema mentioned.
Upvotes: 16
Views: 20911
Reputation: 1375
In case you want to do this with pydantic Version >= 2.0.0 the code would look like this:
from typing import List, Dict, Union
from pydantic import BaseModel, RootModel
class AuthorBookDetails(BaseModel):
numberOfBooks: int
bestBookIds: List[int]
class AuthorInfoCreate(RootModel[Dict[str, Dict]]):
root: Dict[str, AuthorBookDetails]
class ScreenCreate(BaseModel):
description: str
authorInfo: AuthorInfoCreate
Upvotes: 4
Reputation: 9919
From pydantic issue #2100
__root__
is only supported at parent level.
That's why it's not possible to use
class AuthorInfoCreate(BaseModel):
__root__: Dict[str, AuthorBookDetails]
The following workaround is proposed in the above mentioned issue
from typing import Any, Dict, List from pydantic import BaseModel as PydanticBaseModel from pydantic.utils import ROOT_KEY class BaseModel(PydanticBaseModel): def __init__(__pydantic_self__, **data: Any) -> None: if __pydantic_self__.__custom_root_type__ and data.keys() != {ROOT_KEY}: data = {ROOT_KEY: data} super().__init__(**data) ... ...
Now it's possible to use AuthorInfoCreate
model in the usual way
aic = AuthorInfoCreate(**{"KISHAN": kishan, "BALRAM": balram, "RAM": ram})
PR #2237 should make the workaround above unnecessary.
Upvotes: 0
Reputation: 2566
Actually you should remove Type from type annotations. You need an instance of a class, not an actual class. Try the solution below:
from typing import List,Dict
from pydantic import BaseModel
class AuthorBookDetails(BaseModel):
numberOfBooks: int
bestBookIds: List[int]
class AuthorInfoCreate(BaseModel):
__root__: Dict[str, AuthorBookDetails]
class ScreenCreate(BaseModel):
description: str
authorInfo: AuthorInfoCreate
Upvotes: 9
Reputation: 3654
For those with how to Initialize Pydantic classes, with the answer given by @SKhalymon,
from typing import List, Dict
from pydantic import BaseModel
class AuthorBookDetails(BaseModel):
numberOfBooks: int
bestBookIds: List[int]
class AuthorInfoCreate(BaseModel):
__root__: Dict[str, AuthorBookDetails]
class ScreenCreate(BaseModel):
description: str
authorInfo: AuthorInfoCreate
kishan = AuthorBookDetails( numberOfBooks = 10, bestBookIds = [0, 2, 3, 7])
balram = AuthorBookDetails( numberOfBooks = 15, bestBookIds = [10, 12, 14])
ram = AuthorBookDetails( numberOfBooks = 6, bestBookIds = [3, 5])
aic = AuthorInfoCreate(__root__={"KISHAN": kishan, "BALRAM": balram, "RAM": ram})
sc = ScreenCreate( description = "Best Authors And Their Books", authorInfo = aic)
print(sc.json())
Output:
{"description": "Best Authors And Their Books", "authorInfo": {"__root__": {"KISHAN": {"numberOfBooks": 10, "bestBookIds": [0, 2, 3, 7]}, "BALRAM": {"numberOfBooks": 15, "bestBookIds": [10, 12, 14]}, "RAM": {"numberOfBooks": 6, "bestBookIds": [3, 5]}}}}
Upvotes: 4