-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated decorator implementation with parameter for default ID genera…
…tion function.
- Loading branch information
1 parent
c514649
commit 6486522
Showing
5 changed files
with
89 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,13 @@ | ||
import uuid | ||
from typing import Any | ||
|
||
from pydantic import BaseModel | ||
|
||
from . import saveable | ||
|
||
|
||
@saveable(field_as_id='id', default_id=lambda _: str(uuid.uuid4())) | ||
class ModelWithID(BaseModel): | ||
""" | ||
Base class for models with `id` field prefilled with random UUID if not initialized. | ||
""" | ||
id: str = None | ||
|
||
def __init__(self, **data: Any) -> None: | ||
""" | ||
Initialize a model | ||
:param data: model fields | ||
""" | ||
super().__init__(**data) | ||
if self.id is None: | ||
self.id = str(uuid.uuid4()) | ||
|
||
def __my_id__(self): | ||
return self.id | ||
|
||
def __json__(self): | ||
return self.model_dump_json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters