【发布时间】:2021-12-03 10:25:33
【问题描述】:
我有class CRUDBase(和tiangolo example完全一样)和很多继承了这个基类的类。
我想为这个类添加可选模式:(这个选项不起作用)
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType, Optional[ValidateSchemaType]=None]):
做这样的功能:
def validate(self, db_obj):
if ValidateSchemaType is None:
raise NotImplementedError
try:
ValidateSchemaType.from_orm(db_obj)
except ValidationError as e:
return translate_error(e)
return {}
你有什么想法吗?
【问题讨论】:
-
我认为你应该使用type variables 和Generic。我不清楚你想做什么。
标签: python oop generics fastapi pydantic