Changeset 120

Show
Ignore:
Timestamp:
06/12/07 20:19:26 (6 years ago)
Author:
bbangert
Message:

Adding select_by helper function to query associables for columns from the associated table.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/ext/associable.py

    r118 r120  
    158158            entity._descriptor.add_field(field) 
    159159            entity._descriptor.relationships[able_name] = self 
     160             
     161            def select_by(cls, **kwargs): 
     162                return cls.query().join(attr_name).join('targets').filter_by(**kwargs).list() 
     163            setattr(entity, 'select_by_%s' % self.name, classmethod(select_by)) 
    160164         
    161         def setup(self):         
     165        def setup(self): 
    162166            self.create_properties() 
    163167            return True 
     
    167171            entity.mapper.add_property(attr_name, sa.relation(GenericAssoc, lazy=self.lazy, 
    168172                                       backref='_backref_%s' % entity.table.name)) 
     173            entity.mapper.add_property(self.name, sa.synonym(attr_name)) 
    169174            if self.uselist: 
    170175                def get(self): 
     
    184189                    getattr(self, attr_name).targets = [value] 
    185190                setattr(entity, self.name, property(get, set)) 
    186          
     191                 
    187192    sa.mapper(GenericAssoc, association_table, properties={ 
    188193        'targets':sa.relation(entity, secondary=association_to_table,