So far I have set up the following:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
sent_messages = relationship("Message", backref="sender")
received_messages_association = relationship(RecipientAssociation,
backref="user")
received_messages = association_proxy("received_messages_association",
"message",
creator=MediaAssociation.creator("user"))
class Group(Base):
__tablename__ = "groups"
id = Column(Integer, primary_key=True)
received_messages =
association_proxy("received_messages_association", "message",
creator=MediaAssociation.creator("group"))
class RecipientAssociation(Base):
__tablename__ = "recipient_associations"
id = Column(Integer, primary_key=True)
@classmethod
def creator(cls, discriminator):
"""Provide a 'creator' function to use with
the association proxy."""
return lambda recipient: RecipientAssociation(
recipient=recipient,
discriminator=discriminator)
discriminator = Column(String)
"""Refers to the type of recipient."""
@property
def recipient(self):
"""Return the recipient object."""
return getattr(self, self.discriminator)
This works fine for a one-to-one i have in the same project (user/group
with a picture, with uselist=False on backref), and it's from an article
zzzeek wrote on his blog. See the original source here
<http://techspot.zzzeek.org/files/2007/discriminator_on_association.py>.
Is there a better way to do this at all, and if there is not, what is
missing here for this to work? Am I completely thinking in the wrong
direction?
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.