On 7/4/15 3:56 PM, Fridtjof Mund wrote:

So far I have set up the following:

|classUser(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"))classGroup(Base):__tablename__ ="groups"id =Column(Integer,primary_key=True)received_messages =association_proxy("received_messages_association","message",creator=MediaAssociation.creator("group"))classRecipientAssociation(Base):__tablename__ ="recipient_associations"id =Column(Integer,primary_key=True)@classmethoddefcreator(cls,discriminator):"""Provide a 'creator' function to use with the association proxy."""returnlambdarecipient:RecipientAssociation(recipient=recipient,discriminator=discriminator)discriminator =Column(String)"""Refers to the type of recipient."""@propertydefrecipient(self):"""Return the recipient object."""returngetattr(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?

this is only a fragment (what's "Message"? What are the subtypes we're dealing with?) so I don't really know what the goal is, that said the 2007 article was converted to an updated and enhanced series of examples at http://docs.sqlalchemy.org/en/rel_1_0/orm/examples.html#module-examples.generic_associations.



--
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 sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to