module documentation

Defines instrumentation for class attributes and their interaction with instances.

This module is usually not directly visible to user applications, but defines a large part of the ORM's interactivity.

Class ​Attribute​Event A token propagated throughout the course of a chain of attribute events.
Class ​History A 3-tuple of added, unchanged and deleted values, representing the changes which have occurred on an instrumented attribute.
Class ​Instrumented​Attribute Class bound instrumented attribute which adds basic :term:`descriptor` methods.
Class ​Mapped Represent an ORM mapped :term:`descriptor` attribute for typing purposes.
Class ​Queryable​Attribute No summary
Function del​_attribute Delete the value of an attribute, firing history events.
Function flag​_dirty Mark an instance as 'dirty' without any specific attribute mentioned.
Function flag​_modified Mark an attribute on an instance as 'modified'.
Function get​_attribute Get the value of an attribute, firing any callables required.
Function get​_history Return a .History record for the given object and attribute key.
Function init​_collection Initialize a collection attribute and return the collection adapter.
Function set​_attribute Set the value of an attribute, firing history events.
Function set​_committed​_value Set the value of an attribute with no history events.
Constant HISTORY​_BLANK Undocumented
Constant NO​_KEY Undocumented
Constant OP​_APPEND Undocumented
Constant OP​_BULK​_REPLACE Undocumented
Constant OP​_MODIFIED Undocumented
Constant OP​_REMOVE Undocumented
Constant OP​_REPLACE Undocumented
Variable ​Has​Entity​Namespace Undocumented
Class ​Attribute​Impl internal implementation for instrumented attributes.
Class ​Collection​Attribute​Impl A collection-holding attribute that instruments changes in membership.
Class ​No​Key Undocumented
Class ​Scalar​Attribute​Impl represents a scalar value-holding InstrumentedAttribute.
Class ​Scalar​Object​Attribute​Impl represents a scalar-holding InstrumentedAttribute, where the target object is also instrumented.
Function ​_queryable​_attribute​_unreduce Undocumented
Function backref​_listeners Apply listeners to synchronize a two-way relationship.
Function create​_proxied​_attribute Create an QueryableAttribute / user descriptor hybrid.
Function get​_state​_history Undocumented
Function has​_parent TODO
Function init​_state​_collection Initialize a collection attribute and return the collection adapter.
Function register​_attribute Undocumented
Function register​_attribute​_impl Undocumented
Function register​_descriptor Undocumented
Function unregister​_attribute Undocumented
Constant ​_NO​_HISTORY Undocumented
Constant ​_NO​_STATE​_SYMBOLS Undocumented
Constant ​_T Undocumented
Variable _​Generic_​T Undocumented
def del_attribute(instance, key):

Delete the value of an attribute, firing history events.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy.

def flag_dirty(instance):

Mark an instance as 'dirty' without any specific attribute mentioned.

This is a special operation that will allow the object to travel through the flush process for interception by events such as .SessionEvents.before_flush. Note that no SQL will be emitted in the flush process for an object that has no changes, even if marked dirty via this method. However, a .SessionEvents.before_flush handler will be able to see the object in the .Session.dirty collection and may establish changes on it, which will then be included in the SQL emitted.

New in version 1.2.

See Also

.attributes.flag_modified

def flag_modified(instance, key):

Mark an attribute on an instance as 'modified'.

This sets the 'modified' flag on the instance and establishes an unconditional change event for the given attribute. The attribute must have a value present, else an .InvalidRequestError is raised.

To mark an object "dirty" without referring to any specific attribute so that it is considered within a flush, use the .attributes.flag_dirty call.

See Also

.attributes.flag_dirty

def get_attribute(instance, key):

Get the value of an attribute, firing any callables required.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to make usage of attribute state as understood by SQLAlchemy.

def get_history(obj, key, passive=PASSIVE_OFF):

Return a .History record for the given object and attribute key.

This is the pre-flush history for a given attribute, which is reset each time the .Session flushes changes to the current database transaction.

Note

Prefer to use the .AttributeState.history and .AttributeState.load_history accessors to retrieve the .History for instance attributes.

See Also

.AttributeState.history

.AttributeState.load_history - retrieve history using loader callables if the value is not locally present.

Parameters
objan object whose class is instrumented by the attributes package.
keystring attribute name.
passiveindicates loading behavior for the attribute if the value is not already present. This is a bitflag attribute, which defaults to the symbol .PASSIVE_OFF indicating all necessary SQL should be emitted.
def init_collection(obj, key):

Initialize a collection attribute and return the collection adapter.

This function is used to provide direct access to collection internals for a previously unloaded attribute. e.g.:

collection_adapter = init_collection(someobject, 'elements')
for elem in values:
    collection_adapter.append_without_event(elem)

For an easier way to do the above, see ~sqlalchemy.orm.attributes.set_committed_value.

Parameters
obja mapped object
keystring attribute name where the collection is located.
def set_attribute(instance, key, value, initiator=None):

Set the value of an attribute, firing history events.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy.

Parameters
instancethe object that will be modified
keystring name of the attribute
valuevalue to assign
initiator

an instance of .Event that would have been propagated from a previous event listener. This argument is used when the .set_attribute function is being used within an existing event listening function where an .Event object is being supplied; the object may be used to track the origin of the chain of events.

New in version 1.2.3.
def set_committed_value(instance, key, value):

Set the value of an attribute with no history events.

Cancels any previous history present. The value should be a scalar value for scalar-holding attributes, or an iterable for any collection-holding attribute.

This is the same underlying method used when a lazy loader fires off and loads additional data from the database. In particular, this method can be used by application code which has loaded additional attributes or collections through separate queries, which can then be attached to an instance as though it were part of its original loaded state.

HISTORY_BLANK =

Undocumented

Value
History(None, None, None)
NO_KEY =

Undocumented

Value
NoKey('no name')
OP_APPEND =

Undocumented

Value
util.symbol('APPEND')
OP_BULK_REPLACE =

Undocumented

Value
util.symbol('BULK_REPLACE')
OP_MODIFIED =

Undocumented

Value
util.symbol('MODIFIED')
OP_REMOVE =

Undocumented

Value
util.symbol('REMOVE')
OP_REPLACE =

Undocumented

Value
util.symbol('REPLACE')
HasEntityNamespace =

Undocumented

def _queryable_attribute_unreduce(key, mapped_class, parententity, entity):

Undocumented

def backref_listeners(attribute, key, uselist):
Apply listeners to synchronize a two-way relationship.
def create_proxied_attribute(descriptor):

Create an QueryableAttribute / user descriptor hybrid.

Returns a new QueryableAttribute type that delegates descriptor behavior and getattr() to the given descriptor.

def get_state_history(state, key, passive=PASSIVE_OFF):

Undocumented

def has_parent(cls, obj, key, optimistic=False):
TODO
def init_state_collection(state, dict_, key):

Initialize a collection attribute and return the collection adapter.

Discards any existing collection which may be there.

def register_attribute(class_, key, **kw):

Undocumented

def register_attribute_impl(class_, key, uselist=False, callable_=None, useobject=False, impl_class=None, backref=None, **kw):

Undocumented

def register_descriptor(class_, key, comparator=None, parententity=None, doc=None):

Undocumented

def unregister_attribute(class_, key):

Undocumented

_NO_HISTORY =

Undocumented

Value
util.symbol('NO_HISTORY')
_NO_STATE_SYMBOLS =

Undocumented

Value
frozenset([id(PASSIVE_NO_RESULT), id(NO_VALUE)])
_T =

Undocumented

Value
TypeVar('_T')
_Generic_T =

Undocumented