you want to flatten the scalar attributes to combine the namespace of OrgModel and OrgMemberModel into one. there's no magic there you just have to proxy them all, here's "name":
class OrgMemberModel(Base):
__tablename__ = 'org_member'
user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
org_id = Column(Integer, ForeignKey('org.id'), primary_key=True)
active = Column(Boolean, nullable=False, default=True)
role = Column(String(64), nullable=False, default='member')
user = relationship('UserModel',
backref=backref('org_membership', cascade='all,
delete-orphan'))
org = relationship('OrgModel')
name = association_proxy("org", "name")
u1 = UserModel()
u1.org_membership.append(OrgMemberModel(role='r1', org=OrgModel(name='o1')))
assert u1.org_membership[0].role == 'r1'
assert u1.org_membership[0].name == 'o1'
u1.org_membership[0].name = 'o2'
assert u1.org_membership[0].name == 'o2'
On Aug 4, 2013, at 4:03 AM, AM <[email protected]> wrote:
> On 08/03/2013 09:38 AM, Michael Bayer wrote:
>> On Aug 2, 2013, at 2:32 PM, Ams Fwd <[email protected]> wrote:
>>
>>> Hi All.
>>>
>>> Is there any way to transparently add the properties of the associated
>>> object to the association proxy?
>>>
>>> For e.g. if I have the following setup
>>>
>>> class UserModel(Base):
>>> name = sa.Column(sa.String(512))
>>> orgs = association_proxy('org_member', 'org')
>>>
>>>
>>> class OrgModel(Base):
>>> name = sa.Column(sa.String(256), unique=True)
>>>
>>> class OrgMemberModel(Base):
>>> __tablename__ = 'org_member'
>>>
>>> user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'),
>>> primary_key=True)
>>> org_id = sa.Column(sa.Integer, sa.ForeignKey('org.id'),
>>> primary_key=True)
>>> active = sa.Column(sa.Boolean, nullable=False, default=True)
>>> role = sa.Column(sa.String(64), nullable=False, default='member')
>>>
>>> user = relationship('UserModel', backref=backref('org_membership',
>>> cascade='all, delete-orphan'))
>>> org = relationship('OrgModel')
>>>
>>> What I would like to be able to do is access the properties of the OrgModel
>>> & OrgMemberModel via the org_membership[i] object.
>>>
>>> Currently I have to do the following:
>>>
>>> user.org_membership[0].organization.name
>>> user.org_membership[0].role
>>>
>>> Ideally I would like to be able to do:
>>>
>>> user.org_membership[0].name
>>> user.org_membership[0].role
>>>
>>> Any help would be greatly appreciated.
>>
>> on UserModel you'd have an association proxy from "org_membership" to "org".
>> Not sure what "org_member" is.
>
> Hi Michael.
>
> I think that is what I have. org_member is the many-to-many relation table
> between User and Org. org_membership in this case is the backref from the
> relation into the user table.
>
> My problem is that the backref (as expected) provides the relation property
> and another property that points to the org at the other end of the relation
> between user and org.
>
> What I want is a technique that does not require that extra level of
> indirection:
>
> user->org_membership->org->org_property
>
> and instead simply be able to do:
>
> user->org_membership->org_property.
>
> Thanks.
>
> AM
>
> --
> 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/groups/opt_out.
>
>
signature.asc
Description: Message signed with OpenPGP using GPGMail
