0

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.

1 Answer 1

2

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(...)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.