On most of my tables I have the concept of an owner, a reference to
the user who created the row. On most of my other relations, I need to
filter by owner==current_user.

All the classes that have an owner are marked (separate parent class,
I'm using elixir) so I could probably come up with a general solution.
An idea of how to do this might be to define:

def __getattr__(self, attr):
     rel_attr = attr + '_id'
     field = getattr(self.table.c, rel_attr, None)
     if field and field.foreign_keys:
          rel_table = field.foreign_keys[0].constraint.table
          objs = meta.Session.query(rel_table).filter
(owner_id==current_user.id).all()
          setattr(self, attr, objs) # XXX not sure that this is a good
idea
          return objs

     # XXX quite sure this won't work, maybe need to call super(Me,
self).__getattr__(attr) ?
     raise AttributeError(attr)

If you have some advice on the XXX comments above or an idea to
improve the solution / better solution, please give me a hand.

Thanks,
-Dan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to