Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Pydantic Model Schema with Dynamic Key

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.

like image 456
navule Avatar asked Feb 06 '20 07:02

navule


Video Answer


2 Answers

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
like image 92
skhalymon Avatar answered Oct 13 '22 13:10

skhalymon


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]}}}}
like image 41
navule Avatar answered Oct 13 '22 12:10

navule