Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Inheritance #395

Open
mcleantom opened this issue Dec 21, 2023 · 0 comments
Open

Support Inheritance #395

mcleantom opened this issue Dec 21, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@mcleantom
Copy link

Feature request

Context

My previous models all have fields such as _date_created which I would like to support by having a base document model such as:

class BaseMongoModel(Model):
    date_created: datetime = Field(default_factory=datetime.utcnow)


class Tree(BaseMongoModel):
    name: str
    average_size: float
    discovery_year: int

However when trying to apply something with the engine, I get the error:

TypeError: Can only call find with a Model class

Solution

Implement a base model class and allow inheritance

Alternative solutions

This could be solved using composition, such as:

class Metadata(EmbeddedModel):
    date_created: datetime = Field(default_factory=datetime.utcnow)


class Tree(Model):
    metadata: Metadata = Field(default_factory=Metadata)
    name: str
    average_size: float
    discovery_year: int

However for this use case, I think I would prefer inheritance to ensure that all new models have this data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant