introduce pydantic config dict

This commit is contained in:
Jakub Miazek 2023-07-24 12:53:57 +02:00
parent 603225a0de
commit 0b2d2e7933
2 changed files with 43 additions and 36 deletions

View File

@ -1,9 +1,12 @@
from uuid import UUID from uuid import UUID
from pydantic import BaseModel, Field from pydantic import BaseModel, Field, ConfigDict
config = ConfigDict(from_attributes=True)
class NonsenseSchema(BaseModel): class NonsenseSchema(BaseModel):
model_config = config
name: str = Field( name: str = Field(
title="", title="",
description="", description="",
@ -13,17 +16,18 @@ class NonsenseSchema(BaseModel):
description="", description="",
) )
class Config: # class Config:
from_attributes = True # from_attributes = True
json_schema_extra = { # json_schema_extra = {
"example": { # "example": {
"name": "Name for Some Nonsense", # "name": "Name for Some Nonsense",
"description": "Some Nonsense Description", # "description": "Some Nonsense Description",
} # }
} # }
class NonsenseResponse(BaseModel): class NonsenseResponse(BaseModel):
model_config = config
id: UUID = Field( id: UUID = Field(
title="Id", title="Id",
description="", description="",
@ -37,12 +41,12 @@ class NonsenseResponse(BaseModel):
description="", description="",
) )
class Config: # class Config:
from_attributes = True # from_attributes = True
json_schema_extra = { # json_schema_extra = {
"example": { # "example": {
"config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6", # "config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "Name for Some Nonsense", # "name": "Name for Some Nonsense",
"description": "Some Nonsense Description", # "description": "Some Nonsense Description",
} # }
} # }

View File

@ -1,6 +1,8 @@
from uuid import UUID from uuid import UUID
from pydantic import BaseModel, Field from pydantic import BaseModel, Field, ConfigDict
config = ConfigDict(from_attributes=True)
class StuffSchema(BaseModel): class StuffSchema(BaseModel):
@ -13,17 +15,18 @@ class StuffSchema(BaseModel):
description="", description="",
) )
class Config: # class Config:
from_attributes = True # from_attributes = True
json_schema_extra = { # json_schema_extra = {
"example": { # "example": {
"name": "Name for Some Stuff", # "name": "Name for Some Stuff",
"description": "Some Stuff Description", # "description": "Some Stuff Description",
} # }
} # }
class StuffResponse(BaseModel): class StuffResponse(BaseModel):
model_config = config
id: UUID = Field( id: UUID = Field(
title="Id", title="Id",
description="", description="",
@ -37,12 +40,12 @@ class StuffResponse(BaseModel):
description="", description="",
) )
class Config: # class Config:
from_attributes = True # from_attributes = True
json_schema_extra = { # json_schema_extra = {
"example": { # "example": {
"config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6", # "config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "Name for Some Stuff", # "name": "Name for Some Stuff",
"description": "Some Stuff Description", # "description": "Some Stuff Description",
} # }
} # }