Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

python: create empty dataclass and populate it on demand

$
0
0

python newbie: I need to create an empty data model class, populate it with values and then convert it to JSON string, however i am missing something important here. see error below

my data class:

from pydantic import BaseModelclass ServiceInfo(BaseModel):    SerialNumber: str    OS: strclass ItemInfo(BaseModel):    Name: str    Version: strclass AppInfo(BaseModel):    serviceInfo: ServiceInfo    itemInfo: ItemInfo

this is how i attempt to use the data model. i simplified for brevity, but there is a lot of logic here how this data model gets populated:

def myfunction()    info = AppInfo() # <-- ERROR HERE "2 validation errors for ServiceInfo and ItemInfo"    info.serviceInfo.SerialNumber = GetSerial()    info.serviceInfo.OS = "OS 3.19"    info.itemInfo.Name = GetName()    info.itemInfo.Version = GetVersion()    return info.json

thank you in advance


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>