You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to inherit from ModelSchemas so I can add a configuration layer where I can add custom functionality to the underlying pydantic BaseModel and then inherit this ProjectModelSchema to create my model schemas. I believe the reason this functionality is broken is due to the underlying logic surrounding the use of create_schema in ModelSchemaMetaclass which stops any inheritance chain using ModelSchema. Multiple inheritance works in some cases, but I'd like to stay away from that.
Desire:
classProjectModelSchema(ModelSchema):
_custom_serializer=pydantic.model_serializer(mode="wrap")(_project_custom_serializer)
classItemModelSchema(ProjectModelSchema):
# custom serializer is inheritedcustomfield: Optional[str] =NoneclassMeta:
model=Itemfields= ["id", "name"]
classChildItemModelSchema(ItemModelSchema):
# custom serializer and custom field are inheritedclassMeta(ItemModelSchema.Meta):
model=SubItemfields=ItemModelSchema.Meta.fields+ ["childfield"]
Problems:
You actually can't inherit from ModelSchema more than one deep. Example:
classItemModelSchema(ModelSchema):
classMeta:
model=Itemfields= ["id", "slug"]
classChildItemModelSchema(ItemModelSchema):
# this Meta is completely ignoredclassMeta:
model=Itemfields= ["id", "slug", "name"]
You cannot currently create a ModelSchema class without specifying an internal Meta class which completely blocks the desired inheritance without multiple inheritance.
If I missed something please point it out.
The text was updated successfully, but these errors were encountered:
I would like to be able to inherit from ModelSchemas so I can add a configuration layer where I can add custom functionality to the underlying pydantic
BaseModel
and then inherit thisProjectModelSchema
to create my model schemas. I believe the reason this functionality is broken is due to the underlying logic surrounding the use of create_schema inModelSchemaMetaclass
which stops any inheritance chain usingModelSchema
. Multiple inheritance works in some cases, but I'd like to stay away from that.Desire:
Problems:
If I missed something please point it out.
The text was updated successfully, but these errors were encountered: