SQLAlchemy 0.4 Documentation - module sqlalchemy.ext.associationproxy

# SQLAlchemy 0.4 Documentation

Multiple Pages | One Page
Version: 0.4.6 Last Updated: 05/14/08 14:36:43

### module sqlalchemy.ext.associationproxy

Contain the `AssociationProxy` class.

The `AssociationProxy` is a Python property object which provides transparent proxied access to the endpoint of an association object.

See the example `examples/association/proxied_association.py`.

### Module Functions

def association_proxy(targetcollection, attr, **kw)

Convenience function for use in mapped classes. Implements a Python property representing a relation as a collection of simpler values. The proxied property will mimic the collection type of the target (list, dict or set), or in the case of a one to one relation, a simple scalar value.

targetcollection
  Name of the relation attribute we'll proxy to, usually created with 'relation()' in a mapper setup.
attr

  Attribute on the associated instances we'll proxy for. For example, given a target collection of [obj1, obj2], a list created by this proxy property would look like [getattr(obj1, attr), getattr(obj2, attr)]

  If the relation is one-to-one or otherwise uselist=False, then simply: getattr(obj, attr)

creator (optional)

  When new items are added to this proxied collection, new instances of the class collected by the target collection will be created. For list and set collections, the target class constructor will be called with the 'value' for the new instance. For dict types, two arguments are passed: key and value.

  If you want to construct instances differently, supply a 'creator' function that takes arguments as above and returns instances.

  For scalar relations, creator() will be called if the target is None. If the target is present, set operations are proxied to setattr() on the associated object.

  If you have an associated object with multiple attributes, you may set up multiple association proxies mapping to different attributes. See the unit tests for examples, and for examples of how creator() functions can be used to construct the scalar relation on-demand in this situation.

Passes along any other arguments to AssociationProxy

### class AssociationProxy(object)

A property object that automatically sets up AssociationLists on an object.

def __init__(self, targetcollection, attr, creator=None, getset_factory=None, proxy_factory=None, proxy_bulk_set=None)

Arguments are:

targetcollection
  Name of the collection we'll proxy to, usually created with 'relation()' in a mapper setup.
attr
  Attribute on the collected instances we'll proxy for. For example, given a target collection of [obj1, obj2], a list created by this proxy property would look like [getattr(obj1, attr), getattr(obj2, attr)]
creator

  Optional. When new items are added to this proxied collection, new instances of the class collected by the target collection will be created. For list and set collections, the target class constructor will be called with the 'value' for the new instance. For dict types, two arguments are passed: key and value.

  If you want to construct instances differently, supply a 'creator' function that takes arguments as above and returns instances.

getset_factory

  Optional. Proxied attribute access is automatically handled by routines that get and set values based on the attr argument for this proxy.

  If you would like to customize this behavior, you may supply a getset_factory callable that produces a tuple of getter and setter functions. The factory is called with two arguments, the abstract type of the underlying collection and this proxy instance.

proxy_factory
  Optional. The type of collection to emulate is determined by sniffing the target collection. If your collection type can't be determined by duck typing or you'd like to use a different collection implementation, you may supply a factory function to produce those collections. Only applicable to non-scalar relations.
proxy_bulk_set
  Optional, use with proxy_factory. See the _set() method for details.

target_class = property()
def __delete__(self, obj)
def __get__(self, obj, class_)
def __set__(self, obj, values)
back to section top