Package pyperry :: Module association :: Class HasManyThrough
[frames] | no frames]

Class HasManyThrough

source code

 object --+        
          |        
Association --+    
              |    
            Has --+
                  |
                 HasManyThrough

The HasManyThrough class is used whenever a has_many association is created with the through option. It works by using the association id given with the through option as a proxy association to another class on which a source association is defined. The source class for a has many through association will be the source class of the source association. This means that the scope for a has many through association is actually two scopes chained together. The proxy scope is used to retrieve the records on which to build the source association, which is used to build a scope for the records represented by the has many through association. The proxy and source associations may be any of the simple has or belongs to association types.

Options specific to has many through associations

Has many through example

This example shows how to create a basic has many through relationship in which the internet has many connected devices through its networks. The has many through association is defined on the Internet class. Notice how the proxy association, networks, is defined on the target class, Internet, and the source association, devices, is defined on the proxy association's source class, Network. Therefore, the source class for the entire relation is the source association's source class, Device:

   class Internet(pyperry.Base):
       def _config(cls):
           cls.attributes('id')
           cls.has_many('connected_devices', through='networks', source='devices')
           cls.has_many('networks', class_name='Network')

   class Network(pyperry.Base):
       def _config(cls):
           cls.attributes('id', 'internet_id')
           cls.belongs_to('internet', class_name='Internet')
           cls.has_many('devices', class_name='Device')

   class Device(pyperry.Base):
       def _config(cls):
           cls.attributes('id', 'network_id')
           cls.belongs_to('network', class_name='Network')
Instance Methods
 
type(self) source code
 
collection(self) source code
 
polymorphic(self) source code
 
proxy_association(self) source code
 
source_association(self) source code
 
source_klass(self) source code
 
scope(self, obj)
Returns a scope on the source class containing this association
source code

Inherited from Has: get_foreign_key, polymorphic_type, set_foreign_key

Inherited from Association: __call__, __init__, eager_loadable, finder_options, primary_key

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from Has: foreign_key

Inherited from object: __class__

Method Details

type(self)

source code 
Overrides: Association.type

collection(self)

source code 
Overrides: Association.collection

polymorphic(self)

source code 
Overrides: Association.polymorphic

source_klass(self)

source code 
Overrides: Association.source_klass

scope(self, obj)

source code 

Returns a scope on the source class containing this association

Builds conditions on top of the base_scope generated from any finder options set with the association:

   has_many('widgets', klass=Widget, foreign_key='widget_id')
   has_many('comments', as_='parent')

In addition to any finder options included with the association options the following will be added:

   where('widget_id = %s ' % target['id'])

Or for the polymorphic :comments association:

   where('parent_id = %s AND parent_type = %s' % (target['id'],
   target.class))
Overrides: Association.scope