On Oct 28, 2010, at 5:20 AM, Torsten Landschoff wrote:

> Hi Michael,
> 
> Am Mittwoch, den 27.10.2010, 13:37 -0400 schrieb Michael Bayer:
> 
> 
>> Configuration is too unwieldy ?   Since you're using declarative ,
>> just using @declared_attr would give you access to the class:
> 
> Nice one. I missed classproperty and declared_attr so far.
> 
>> def make_ordered_list(key, pk, type_):
>>    @declared_attr
>>    def go(cls):
>>        class Name(Base):
>>            __tablename__ = "%s_names" % key
>>            rel_id = Column("related_id", Integer, ForeignKey(pk), 
>>                            primary_key=True, index=True)
>>            position = Column("position",  Integer)
>>            value = Column("value", type_, primary_key=True)
>>            def __init__(self, value):
>>                self.value = value
>>        private_key = "_" + key
>> 
>>        setattr(cls, key, association_proxy(private_key, "value"))
> 
> Now that's a hefty trick, updating the class when reading the property.
> I think this should be protected against running twice!? Or does the ORM
> mapper do this for us?

the interpretation of @declared_attr is done by the declarative module, so the 
metaclass fires up when you create the class, the attributes are scanned 
including those that are of @declared_attr, at which point they are invoked, 
and the result is put into the mapping.  Then the mapper is constructed which 
immediately replaces your function with an ORM instrumented attribute.   So 
basically yes its a one shot.


-- 
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