mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-15 04:40:36 +02:00
fix: added string method for base model
This commit is contained in:
@@ -19,10 +19,21 @@ class Base:
|
|||||||
|
|
||||||
__name__: str
|
__name__: str
|
||||||
|
|
||||||
# TODO: Add default representation string
|
|
||||||
# TODO: Add automated foreign key resolution
|
# TODO: Add automated foreign key resolution
|
||||||
|
|
||||||
# Generate __tablename__ automatically
|
# Generate __tablename__ automatically
|
||||||
@declared_attr
|
@declared_attr
|
||||||
def __tablename__(cls) -> str:
|
def __tablename__(cls) -> str:
|
||||||
return cls.__name__.lower()
|
return cls.__name__.lower()
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
# if the object has a name, title or similar attribute, return it
|
||||||
|
if hasattr(self, "name"):
|
||||||
|
return str(self.name) # type: ignore
|
||||||
|
|
||||||
|
# if the object has a title attribute, return it
|
||||||
|
if hasattr(self, "title"):
|
||||||
|
return str(self.title) # type: ignore
|
||||||
|
|
||||||
|
# otherwise return the object's id
|
||||||
|
return str(self.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user