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

Class Association

source code

object --+
         |
        Association
Known Subclasses:

Associations

Associations allow you to retrieve a model or collection of models that are related to one another in some way. Here we are concerned with how associations are defined and implemented. For documentation on how to use associations in your models, please see the belongs_to, has_one, and has_many methods on pyperry.Base.

Terminology

To understand how associations work, we must define the concepts of a source class and a target class for an association.

An example showing the difference between source and target classes:

   class Car(pyperry.Base): pass
   class Part(pyperry.Base): pass

   # Car is the target class and Part is the source class
   Car.has_many('parts', class_name='Part')

   # Part is the target class and Car is the source class
   Part.belongs_to('car', class_name='Car')

   c = Car.first()
   # returns a collection of Part models (Part is the source class)
   c.parts()

   p = Part.first()
   # returns a Car model (Car is the source class)
   p.car()

What happens when you define an association

Now let's look at what happens when you define an association on a model. We will use the association Car.has_many('parts', class_name='Part') as an example because all associations work in the same general way. In this example, a HasMany class (an Association subclass) is instantiated where Car is given as the target_klass argument, and 'parts' is given as the id argument. Because we passed in 'Part' for the class_name option, it is used as the source class for this association.

The association id is used as the name of the association method that gets defined on the target class. So in our example, all Car instances now have a parts() method that can be called to retrieve a collection of parts for a car. When you call the association method on the target class, a relation (or scope) is constructed for the source class representing all of the records related to the target class. For associations that represent collections, such as has_many, a relation is returned that you can further modify. For associations that represent a single object, such as belongs_to or has_one, an instance of that model is returned.

In summary, all an association does is create a scope on the source class that represents records from the source class that are related to (or associated with) the target class. Then a method on the target class is created that returns this scope.

This means that calling car.parts() is just returning a scope like:

   Part.scoped().where({'car_id': car.id})

Similarly, calling part.car() is just returning a scope like:

   Car.scoped().where({'id': part.car_id}).first()
Instance Methods
 
__init__(self, target_klass, id, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__call__(self, obj) source code
 
type(self) source code
 
polymorphic(self) source code
 
collection(self) source code
 
scope(self) source code
 
primary_key(self, target_instance=None) source code
 
get_foreign_key(self) source code
 
set_foreign_key(self, value) source code
 
eager_loadable(self) source code
 
finder_options(self) source code
 
source_klass(self, obj=None) source code

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

Properties
  foreign_key

Inherited from object: __class__

Method Details

__init__(self, target_klass, id, **kwargs)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

Property Details

foreign_key

Get Method:
get_foreign_key(self)
Set Method:
set_foreign_key(self, value)