Don't forget that you need a context for the views that map to /users
. Having a single User
class would make it quite difficult, and certainly not elegant.
My guess is that a separate UserContainer
would be the simpler solution you can find. Depending on you database backend, you might even be able to do it in a generic way. For example, with SqlAlchemy:
class Container:
def __getitem__(self, item):
try:
id = int(item)
return DBSession.query(self.cls).filter_by(id=id).one()
except (NoResultFound, ValueError):
raise KeyError(item)
Then, your UserContainer
is just:
class UserContainer(Container):
cls = User
Elegant, isn't it ?