Is it possible to subclass DTOs? #2719
-
I would like to define subclassed DTOs mapping to my internal data structure schema. There I use a BaseRecord, which is subclassed by various records: class BaseRecord(BaseModel):
id: str
updated: str
created: str
class UserRecord(BaseRecord):
name: str
email: str
password: str
... Now I want to create a BaseDTO that includes a configuration for the BaseRecord. In addition, I would like to be able to update this configuration in subclasses, for example, to exclude fields. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I assume that Is there a specific error you are encountering? I believe something like this should work, but its just off the top of my head so might be errors in it: ModelT = TypeVar("ModelT", bound=BaseRecord)
class BaseRecordDTO(PydanticDTO[ModelT]):
config = DTOConfig(rename={"updated": "updatedAt", "created": "createdAt"})
UserRecordDTO = BaseRecordDTO[UserRecord] |
Beta Was this translation helpful? Give feedback.
-
I'm not against this but I still have some doubts regarding the merging of the config. @peterschutt, the current method you're proposed works if the user subclasses from only one parent DTO. What if they subclass from multiple DTOs? I can see a scenario where users would create multiple "base" DTOs with various configurations to then be used as mixins to form the final DTO they want. |
Beta Was this translation helpful? Give feedback.
#2760