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
-
through: the association id of an association defined on the
target class. This association will be used as the proxy association,
and it is an error if this association does not exist.
-
source: the id of the source association. If the source option
is not specified, we assume that the source association's id is the
same as the has many through association's id.
-
source_type: the name of the source class as a string. This
option may be required if the source class is ambiguous, such as when
the source association is a polymorphic belongs_to association.
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__
|
|
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
|