I'm trying to create an entity architecture with SQLAlchemy and python Generics, i.e Model -> Repository -> API
I have the following User model:
class User(db.Model):
id
username
email
...
The following Repository code:
class UserRepository(Repository[User]):
pass
I'm trying to find a way that I could "access" the User model from within the Repository base class, something like:
T = TypeVar('T')
class Repository(Generic[T]):
@staticmethod
def get(id) -> T:
return T.query.filter(...)
Tried also going via db.session.query(...)
without success.
Any ideas?
Thanks.
CodePudding user response:
Managed to solve it with the help of class variables
class XRepository(BaseRepository):
model = X
class YRepository(BaseRepository):
model = Y
class BaseRepository:
@classmethod
def get(cls):
return cls.model.query.filter(...)