From 8019b566f28c4e1ee53f556a909aee36ffdd07d7 Mon Sep 17 00:00:00 2001 From: Conrad Date: Tue, 21 Jan 2025 22:16:07 +0100 Subject: [PATCH] fix: added string method for base model --- creyPY/fastapi/models/base.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/creyPY/fastapi/models/base.py b/creyPY/fastapi/models/base.py index 1b68d10..4319a6c 100644 --- a/creyPY/fastapi/models/base.py +++ b/creyPY/fastapi/models/base.py @@ -19,10 +19,21 @@ class Base: __name__: str - # TODO: Add default representation string # TODO: Add automated foreign key resolution # Generate __tablename__ automatically @declared_attr def __tablename__(cls) -> str: 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)