> Thanks a lot Mike! This gives me interesting paths to research.

For the record, I finally wrote my own association_proxy. This is an
hybrid between SQLAlchemy's association_proxy and hybrid_property.


class _association_proxy(object):
    # A specific "association proxy" implementation

    def __init__(self, target, value_attr):
        self.target = target
        self.value_attr = value_attr

    def __get__(self, obj, type=None):
        if obj is None:
            # For "hybrid" descriptors that work both at the instance
            # and class levels we could return an SQL expression here.
            # The code of hybrid_property in SQLAlchemy illustrates
            # how to do that.
            raise AttributeError
        return getattr(getattr(obj, self.target), self.value_attr)

    def __set__(self, obj, val):
        o = getattr(obj, self.target)
        # if the obj as no child object or if the child object
        # does not correspond to the new value then we need to
        # read a new child object from the database
        if not o or getattr(o, self.value_attr) != val:
            relationship_property = class_mapper(obj.__class__) \
                .get_property(self.target)
            child_cls = relationship_property.argument
            o = Session.query(child_cls).filter(
                    getattr(child_cls, self.value_attr) == val).first()
            setattr(obj, self.target, o)



Thanks again Mike for your support.

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : [email protected]
http://www.camptocamp.com

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to