<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Deprecated ORM Event Interfaces — SQLAlchemy 0.8 Documentation </title> <!-- begin iterate through SQLA + sphinx environment css_files --> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../_static/docs.css" type="text/css" /> <link rel="stylesheet" href="../_static/changelog.css" type="text/css" /> <link rel="stylesheet" href="../_static/sphinx_paramlinks.css" type="text/css" /> <!-- end iterate through SQLA + sphinx environment css_files --> <!-- begin layout.mako headers --> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '0.8.7', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html' }; </script> <!-- begin iterate through sphinx environment script_files --> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <!-- end iterate through sphinx environment script_files --> <script type="text/javascript" src="../_static/detectmobile.js"></script> <script type="text/javascript" src="../_static/init.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="SQLAlchemy 0.8 Documentation" href="../index.html" /> <!-- end layout.mako headers --> </head> <body> <div id="docs-container"> <div id="docs-top-navigation-container" class="body-background"> <div id="docs-header"> <div id="docs-version-header"> Release: <span class="version-num">0.8.7</span> | Release Date: July 22, 2014 </div> <h1>SQLAlchemy 0.8 Documentation</h1> </div> </div> <div id="docs-body-container"> <div id="fixed-sidebar" class="withsidebar"> <div id="docs-sidebar-popout"> <h3><a href="../index.html">SQLAlchemy 0.8 Documentation</a></h3> <p id="sidebar-paginate"> <a href="../index.html" title="SQLAlchemy 0.8 Documentation">Up</a> | </p> <p id="sidebar-topnav"> <a href="../index.html">Contents</a> | <a href="../genindex.html">Index</a> </p> <div id="sidebar-search"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="12" /> <input type="submit" value="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div id="docs-sidebar"> <h3><a href="#"> Deprecated ORM Event Interfaces </a></h3> <ul> <li><a class="reference internal" href="#">Deprecated ORM Event Interfaces</a><ul> <li><a class="reference internal" href="#mapper-events">Mapper Events</a></li> <li><a class="reference internal" href="#session-events">Session Events</a></li> <li><a class="reference internal" href="#attribute-events">Attribute Events</a></li> </ul> </li> </ul> </div> </div> <div id="docs-body" class="withsidebar" > <div class="section" id="module-sqlalchemy.orm.interfaces"> <span id="deprecated-orm-event-interfaces"></span><span id="dep-interfaces-orm-toplevel"></span><h1>Deprecated ORM Event Interfaces<a class="headerlink" href="#module-sqlalchemy.orm.interfaces" title="Permalink to this headline">¶</a></h1> <p>This section describes the class-based ORM event interface which first existed in SQLAlchemy 0.1, which progressed with more kinds of events up until SQLAlchemy 0.5. The non-ORM analogue is described at <a class="reference internal" href="../core/interfaces.html"><em>Deprecated Event Interfaces</em></a>.</p> <div class="deprecated"> <p><span>Deprecated since version 0.7: </span>As of SQLAlchemy 0.7, the new event system described in <a class="reference internal" href="../core/event.html"><em>Events</em></a> replaces the extension/proxy/listener system, providing a consistent interface to all events without the need for subclassing.</p> </div> <div class="section" id="mapper-events"> <h2>Mapper Events<a class="headerlink" href="#mapper-events" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="sqlalchemy.orm.interfaces.MapperExtension"> <em class="property">class </em><tt class="descclassname">sqlalchemy.orm.interfaces.</tt><tt class="descname">MapperExtension</tt><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension" title="Permalink to this definition">¶</a></dt> <dd><p>Base implementation for <a class="reference internal" href="mapper_config.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><tt class="xref py py-class docutils literal"><span class="pre">Mapper</span></tt></a> event hooks.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><a class="reference internal" href="#sqlalchemy.orm.interfaces.MapperExtension" title="sqlalchemy.orm.interfaces.MapperExtension"><tt class="xref py py-class docutils literal"><span class="pre">MapperExtension</span></tt></a> is deprecated. Please refer to <a class="reference internal" href="../core/event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a> as well as <a class="reference internal" href="events.html#sqlalchemy.orm.events.MapperEvents" title="sqlalchemy.orm.events.MapperEvents"><tt class="xref py py-class docutils literal"><span class="pre">MapperEvents</span></tt></a>.</p> </div> <p>New extension classes subclass <a class="reference internal" href="#sqlalchemy.orm.interfaces.MapperExtension" title="sqlalchemy.orm.interfaces.MapperExtension"><tt class="xref py py-class docutils literal"><span class="pre">MapperExtension</span></tt></a> and are specified using the <tt class="docutils literal"><span class="pre">extension</span></tt> mapper() argument, which is a single <a class="reference internal" href="#sqlalchemy.orm.interfaces.MapperExtension" title="sqlalchemy.orm.interfaces.MapperExtension"><tt class="xref py py-class docutils literal"><span class="pre">MapperExtension</span></tt></a> or a list of such:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.orm.interfaces</span> <span class="kn">import</span> <span class="n">MapperExtension</span> <span class="k">class</span> <span class="nc">MyExtension</span><span class="p">(</span><span class="n">MapperExtension</span><span class="p">):</span> <span class="k">def</span> <span class="nf">before_insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mapper</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span> <span class="k">print</span> <span class="s">"instance </span><span class="si">%s</span><span class="s"> before insert !"</span> <span class="o">%</span> <span class="n">instance</span> <span class="n">m</span> <span class="o">=</span> <span class="n">mapper</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">users_table</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">MyExtension</span><span class="p">())</span></pre></div> </div> <p>A single mapper can maintain a chain of <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> objects. When a particular mapping event occurs, the corresponding method on each <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> is invoked serially, and each method has the ability to halt the chain from proceeding further:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">m</span> <span class="o">=</span> <span class="n">mapper</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">users_table</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="p">[</span><span class="n">ext1</span><span class="p">,</span> <span class="n">ext2</span><span class="p">,</span> <span class="n">ext3</span><span class="p">])</span></pre></div> </div> <p>Each <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> method returns the symbol EXT_CONTINUE by default. This symbol generally means “move to the next <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> for processing”. For methods that return objects like translated rows or new object instances, EXT_CONTINUE means the result of the method should be ignored. In some cases it’s required for a default mapper activity to be performed, such as adding a new instance to a result list.</p> <p>The symbol EXT_STOP has significance within a chain of <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> objects that the chain will be stopped when this symbol is returned. Like EXT_CONTINUE, it also has additional significance in some cases that a default mapper activity will not be performed.</p> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.after_delete"> <tt class="descname">after_delete</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.after_delete" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance after that instance is deleted.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.after_insert"> <tt class="descname">after_insert</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.after_insert" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance after that instance is inserted.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.after_update"> <tt class="descname">after_update</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.after_update" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance after that instance is updated.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.append_result"> <tt class="descname">append_result</tt><big>(</big><em>mapper</em>, <em>selectcontext</em>, <em>row</em>, <em>instance</em>, <em>result</em>, <em>**flags</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.append_result" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance before that instance is appended to a result list.</p> <p>If this method returns EXT_CONTINUE, result appending will proceed normally. if this method returns any other value or None, result appending will not proceed for this instance, giving this extension an opportunity to do the appending itself, if desired.</p> <dl class="docutils"> <dt>mapper</dt> <dd>The mapper doing the operation.</dd> <dt>selectcontext</dt> <dd>The QueryContext generated from the Query.</dd> <dt>row</dt> <dd>The result row from the database.</dd> <dt>instance</dt> <dd>The object instance to be appended to the result.</dd> <dt>result</dt> <dd>List to which results are being appended.</dd> <dt>**flags</dt> <dd>extra information about the row, same as criterion in <tt class="docutils literal"><span class="pre">create_row_processor()</span></tt> method of <a class="reference internal" href="internals.html#sqlalchemy.orm.interfaces.MapperProperty" title="sqlalchemy.orm.interfaces.MapperProperty"><tt class="xref py py-class docutils literal"><span class="pre">MapperProperty</span></tt></a></dd> </dl> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.before_delete"> <tt class="descname">before_delete</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.before_delete" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance before that instance is deleted.</p> <p>Note that <em>no</em> changes to the overall flush plan can be made here; and manipulation of the <tt class="docutils literal"><span class="pre">Session</span></tt> will not have the desired effect. To manipulate the <tt class="docutils literal"><span class="pre">Session</span></tt> within an extension, use <tt class="docutils literal"><span class="pre">SessionExtension</span></tt>.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.before_insert"> <tt class="descname">before_insert</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.before_insert" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance before that instance is inserted into its table.</p> <p>This is a good place to set up primary key values and such that aren’t handled otherwise.</p> <p>Column-based attributes can be modified within this method which will result in the new value being inserted. However <em>no</em> changes to the overall flush plan can be made, and manipulation of the <tt class="docutils literal"><span class="pre">Session</span></tt> will not have the desired effect. To manipulate the <tt class="docutils literal"><span class="pre">Session</span></tt> within an extension, use <tt class="docutils literal"><span class="pre">SessionExtension</span></tt>.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.before_update"> <tt class="descname">before_update</tt><big>(</big><em>mapper</em>, <em>connection</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.before_update" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance before that instance is updated.</p> <p>Note that this method is called for all instances that are marked as “dirty”, even those which have no net changes to their column-based attributes. An object is marked as dirty when any of its column-based attributes have a “set attribute” operation called or when any of its collections are modified. If, at update time, no column-based attributes have any net changes, no UPDATE statement will be issued. This means that an instance being sent to before_update is <em>not</em> a guarantee that an UPDATE statement will be issued (although you can affect the outcome here).</p> <p>To detect if the column-based attributes on the object have net changes, and will therefore generate an UPDATE statement, use <tt class="docutils literal"><span class="pre">object_session(instance).is_modified(instance,</span> <span class="pre">include_collections=False)</span></tt>.</p> <p>Column-based attributes can be modified within this method which will result in the new value being updated. However <em>no</em> changes to the overall flush plan can be made, and manipulation of the <tt class="docutils literal"><span class="pre">Session</span></tt> will not have the desired effect. To manipulate the <tt class="docutils literal"><span class="pre">Session</span></tt> within an extension, use <tt class="docutils literal"><span class="pre">SessionExtension</span></tt>.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.create_instance"> <tt class="descname">create_instance</tt><big>(</big><em>mapper</em>, <em>selectcontext</em>, <em>row</em>, <em>class_</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.create_instance" title="Permalink to this definition">¶</a></dt> <dd><p>Receive a row when a new object instance is about to be created from that row.</p> <p>The method can choose to create the instance itself, or it can return EXT_CONTINUE to indicate normal object creation should take place.</p> <dl class="docutils"> <dt>mapper</dt> <dd>The mapper doing the operation</dd> <dt>selectcontext</dt> <dd>The QueryContext generated from the Query.</dd> <dt>row</dt> <dd>The result row from the database</dd> <dt>class_</dt> <dd>The class we are mapping.</dd> <dt>return value</dt> <dd>A new object instance, or EXT_CONTINUE</dd> </dl> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.init_failed"> <tt class="descname">init_failed</tt><big>(</big><em>mapper</em>, <em>class_</em>, <em>oldinit</em>, <em>instance</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.init_failed" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an instance when its constructor has been called, and raised an exception.</p> <p>This method is only called during a userland construction of an object. It is not called when an object is loaded from the database.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.init_instance"> <tt class="descname">init_instance</tt><big>(</big><em>mapper</em>, <em>class_</em>, <em>oldinit</em>, <em>instance</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.init_instance" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an instance when its constructor is called.</p> <p>This method is only called during a userland construction of an object. It is not called when an object is loaded from the database.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.instrument_class"> <tt class="descname">instrument_class</tt><big>(</big><em>mapper</em>, <em>class_</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.instrument_class" title="Permalink to this definition">¶</a></dt> <dd><p>Receive a class when the mapper is first constructed, and has applied instrumentation to the mapped class.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.populate_instance"> <tt class="descname">populate_instance</tt><big>(</big><em>mapper</em>, <em>selectcontext</em>, <em>row</em>, <em>instance</em>, <em>**flags</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.populate_instance" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an instance before that instance has its attributes populated.</p> <p>This usually corresponds to a newly loaded instance but may also correspond to an already-loaded instance which has unloaded attributes to be populated. The method may be called many times for a single instance, as multiple result rows are used to populate eagerly loaded collections.</p> <p>If this method returns EXT_CONTINUE, instance population will proceed normally. If any other value or None is returned, instance population will not proceed, giving this extension an opportunity to populate the instance itself, if desired.</p> <div class="deprecated"> <p><span>Deprecated since version 0.5: </span>Most usages of this hook are obsolete. For a generic “object has been newly created from a row” hook, use <tt class="docutils literal"><span class="pre">reconstruct_instance()</span></tt>, or the <tt class="docutils literal"><span class="pre">@orm.reconstructor</span></tt> decorator.</p> </div> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.reconstruct_instance"> <tt class="descname">reconstruct_instance</tt><big>(</big><em>mapper</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.reconstruct_instance" title="Permalink to this definition">¶</a></dt> <dd><p>Receive an object instance after it has been created via <tt class="docutils literal"><span class="pre">__new__</span></tt>, and after initial attribute population has occurred.</p> <p>This typically occurs when the instance is created based on incoming result rows, and is only called once for that instance’s lifetime.</p> <p>Note that during a result-row load, this method is called upon the first row received for this instance. Note that some attributes and collections may or may not be loaded or even initialized, depending on what’s present in the result rows.</p> <p>The return value is only significant within the <tt class="docutils literal"><span class="pre">MapperExtension</span></tt> chain; the parent mapper’s behavior isn’t modified by this method.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.MapperExtension.translate_row"> <tt class="descname">translate_row</tt><big>(</big><em>mapper</em>, <em>context</em>, <em>row</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.MapperExtension.translate_row" title="Permalink to this definition">¶</a></dt> <dd><p>Perform pre-processing on the given result row and return a new row instance.</p> <p>This is called when the mapper first receives a row, before the object identity or the instance itself has been derived from that row. The given row may or may not be a <tt class="docutils literal"><span class="pre">RowProxy</span></tt> object - it will always be a dictionary-like object which contains mapped columns as keys. The returned object should also be a dictionary-like object which recognizes mapped columns as keys.</p> <p>If the ultimate return value is EXT_CONTINUE, the row is not translated.</p> </dd></dl> </dd></dl> </div> <div class="section" id="session-events"> <h2>Session Events<a class="headerlink" href="#session-events" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="sqlalchemy.orm.interfaces.SessionExtension"> <em class="property">class </em><tt class="descclassname">sqlalchemy.orm.interfaces.</tt><tt class="descname">SessionExtension</tt><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension" title="Permalink to this definition">¶</a></dt> <dd><p>Base implementation for <a class="reference internal" href="session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> event hooks.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><a class="reference internal" href="#sqlalchemy.orm.interfaces.SessionExtension" title="sqlalchemy.orm.interfaces.SessionExtension"><tt class="xref py py-class docutils literal"><span class="pre">SessionExtension</span></tt></a> is deprecated. Please refer to <a class="reference internal" href="../core/event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a> as well as <a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents" title="sqlalchemy.orm.events.SessionEvents"><tt class="xref py py-class docutils literal"><span class="pre">SessionEvents</span></tt></a>.</p> </div> <p>Subclasses may be installed into a <a class="reference internal" href="session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> (or <a class="reference internal" href="session.html#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><tt class="xref py py-class docutils literal"><span class="pre">sessionmaker</span></tt></a>) using the <tt class="docutils literal"><span class="pre">extension</span></tt> keyword argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.orm.interfaces</span> <span class="kn">import</span> <span class="n">SessionExtension</span> <span class="k">class</span> <span class="nc">MySessionExtension</span><span class="p">(</span><span class="n">SessionExtension</span><span class="p">):</span> <span class="k">def</span> <span class="nf">before_commit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">session</span><span class="p">):</span> <span class="k">print</span> <span class="s">"before commit!"</span> <span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">extension</span><span class="o">=</span><span class="n">MySessionExtension</span><span class="p">())</span></pre></div> </div> <p>The same <a class="reference internal" href="#sqlalchemy.orm.interfaces.SessionExtension" title="sqlalchemy.orm.interfaces.SessionExtension"><tt class="xref py py-class docutils literal"><span class="pre">SessionExtension</span></tt></a> instance can be used with any number of sessions.</p> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_attach"> <tt class="descname">after_attach</tt><big>(</big><em>session</em>, <em>instance</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_attach" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after an instance is attached to a session.</p> <p>This is called after an add, delete or merge.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_begin"> <tt class="descname">after_begin</tt><big>(</big><em>session</em>, <em>transaction</em>, <em>connection</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_begin" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after a transaction is begun on a connection</p> <p><cite>transaction</cite> is the SessionTransaction. This method is called after an engine level transaction is begun on a connection.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_bulk_delete"> <tt class="descname">after_bulk_delete</tt><big>(</big><em>session</em>, <em>query</em>, <em>query_context</em>, <em>result</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_bulk_delete" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after a bulk delete operation to the session.</p> <p>This is called after a session.query(...).delete()</p> <p><cite>query</cite> is the query object that this delete operation was called on. <cite>query_context</cite> was the query context object. <cite>result</cite> is the result object returned from the bulk operation.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_bulk_update"> <tt class="descname">after_bulk_update</tt><big>(</big><em>session</em>, <em>query</em>, <em>query_context</em>, <em>result</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_bulk_update" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after a bulk update operation to the session.</p> <p>This is called after a session.query(...).update()</p> <p><cite>query</cite> is the query object that this update operation was called on. <cite>query_context</cite> was the query context object. <cite>result</cite> is the result object returned from the bulk operation.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_commit"> <tt class="descname">after_commit</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_commit" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after a commit has occurred.</p> <p>Note that this may not be per-flush if a longer running transaction is ongoing.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_flush"> <tt class="descname">after_flush</tt><big>(</big><em>session</em>, <em>flush_context</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_flush" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after flush has completed, but before commit has been called.</p> <p>Note that the session’s state is still in pre-flush, i.e. ‘new’, ‘dirty’, and ‘deleted’ lists still show pre-flush state as well as the history settings on instance attributes.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_flush_postexec"> <tt class="descname">after_flush_postexec</tt><big>(</big><em>session</em>, <em>flush_context</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_flush_postexec" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after flush has completed, and after the post-exec state occurs.</p> <p>This will be when the ‘new’, ‘dirty’, and ‘deleted’ lists are in their final state. An actual commit() may or may not have occurred, depending on whether or not the flush started its own transaction or participated in a larger transaction.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.after_rollback"> <tt class="descname">after_rollback</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.after_rollback" title="Permalink to this definition">¶</a></dt> <dd><p>Execute after a rollback has occurred.</p> <p>Note that this may not be per-flush if a longer running transaction is ongoing.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.before_commit"> <tt class="descname">before_commit</tt><big>(</big><em>session</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.before_commit" title="Permalink to this definition">¶</a></dt> <dd><p>Execute right before commit is called.</p> <p>Note that this may not be per-flush if a longer running transaction is ongoing.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.SessionExtension.before_flush"> <tt class="descname">before_flush</tt><big>(</big><em>session</em>, <em>flush_context</em>, <em>instances</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.SessionExtension.before_flush" title="Permalink to this definition">¶</a></dt> <dd><p>Execute before flush process has started.</p> <p><cite>instances</cite> is an optional list of objects which were passed to the <tt class="docutils literal"><span class="pre">flush()</span></tt> method.</p> </dd></dl> </dd></dl> </div> <div class="section" id="attribute-events"> <h2>Attribute Events<a class="headerlink" href="#attribute-events" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="sqlalchemy.orm.interfaces.AttributeExtension"> <em class="property">class </em><tt class="descclassname">sqlalchemy.orm.interfaces.</tt><tt class="descname">AttributeExtension</tt><a class="headerlink" href="#sqlalchemy.orm.interfaces.AttributeExtension" title="Permalink to this definition">¶</a></dt> <dd><p>Base implementation for <tt class="xref py py-class docutils literal"><span class="pre">AttributeImpl</span></tt> event hooks, events that fire upon attribute mutations in user code.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><a class="reference internal" href="#sqlalchemy.orm.interfaces.AttributeExtension" title="sqlalchemy.orm.interfaces.AttributeExtension"><tt class="xref py py-class docutils literal"><span class="pre">AttributeExtension</span></tt></a> is deprecated. Please refer to <a class="reference internal" href="../core/event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a> as well as <a class="reference internal" href="events.html#sqlalchemy.orm.events.AttributeEvents" title="sqlalchemy.orm.events.AttributeEvents"><tt class="xref py py-class docutils literal"><span class="pre">AttributeEvents</span></tt></a>.</p> </div> <p><a class="reference internal" href="#sqlalchemy.orm.interfaces.AttributeExtension" title="sqlalchemy.orm.interfaces.AttributeExtension"><tt class="xref py py-class docutils literal"><span class="pre">AttributeExtension</span></tt></a> is used to listen for set, remove, and append events on individual mapped attributes. It is established on an individual mapped attribute using the <cite>extension</cite> argument, available on <a class="reference internal" href="mapper_config.html#sqlalchemy.orm.column_property" title="sqlalchemy.orm.column_property"><tt class="xref py py-func docutils literal"><span class="pre">column_property()</span></tt></a>, <a class="reference internal" href="relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>, and others:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.orm.interfaces</span> <span class="kn">import</span> <span class="n">AttributeExtension</span> <span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">mapper</span><span class="p">,</span> <span class="n">relationship</span><span class="p">,</span> <span class="n">column_property</span> <span class="k">class</span> <span class="nc">MyAttrExt</span><span class="p">(</span><span class="n">AttributeExtension</span><span class="p">):</span> <span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">initiator</span><span class="p">):</span> <span class="k">print</span> <span class="s">"append event !"</span> <span class="k">return</span> <span class="n">value</span> <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">oldvalue</span><span class="p">,</span> <span class="n">initiator</span><span class="p">):</span> <span class="k">print</span> <span class="s">"set event !"</span> <span class="k">return</span> <span class="n">value</span> <span class="n">mapper</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="n">sometable</span><span class="p">,</span> <span class="n">properties</span><span class="o">=</span><span class="p">{</span> <span class="s">'foo'</span><span class="p">:</span><span class="n">column_property</span><span class="p">(</span><span class="n">sometable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">foo</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">MyAttrExt</span><span class="p">()),</span> <span class="s">'bar'</span><span class="p">:</span><span class="n">relationship</span><span class="p">(</span><span class="n">Bar</span><span class="p">,</span> <span class="n">extension</span><span class="o">=</span><span class="n">MyAttrExt</span><span class="p">())</span> <span class="p">})</span></pre></div> </div> <p>Note that the <a class="reference internal" href="#sqlalchemy.orm.interfaces.AttributeExtension" title="sqlalchemy.orm.interfaces.AttributeExtension"><tt class="xref py py-class docutils literal"><span class="pre">AttributeExtension</span></tt></a> methods <a class="reference internal" href="#sqlalchemy.orm.interfaces.AttributeExtension.append" title="sqlalchemy.orm.interfaces.AttributeExtension.append"><tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt></a> and <a class="reference internal" href="#sqlalchemy.orm.interfaces.AttributeExtension.set" title="sqlalchemy.orm.interfaces.AttributeExtension.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> need to return the <tt class="docutils literal"><span class="pre">value</span></tt> parameter. The returned value is used as the effective value, and allows the extension to change what is ultimately persisted.</p> <p>AttributeExtension is assembled within the descriptors associated with a mapped class.</p> <dl class="attribute"> <dt id="sqlalchemy.orm.interfaces.AttributeExtension.active_history"> <tt class="descname">active_history</tt><em class="property"> = True</em><a class="headerlink" href="#sqlalchemy.orm.interfaces.AttributeExtension.active_history" title="Permalink to this definition">¶</a></dt> <dd><p>indicates that the set() method would like to receive the ‘old’ value, even if it means firing lazy callables.</p> <p>Note that <tt class="docutils literal"><span class="pre">active_history</span></tt> can also be set directly via <a class="reference internal" href="mapper_config.html#sqlalchemy.orm.column_property" title="sqlalchemy.orm.column_property"><tt class="xref py py-func docutils literal"><span class="pre">column_property()</span></tt></a> and <a class="reference internal" href="relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.AttributeExtension.append"> <tt class="descname">append</tt><big>(</big><em>state</em>, <em>value</em>, <em>initiator</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.AttributeExtension.append" title="Permalink to this definition">¶</a></dt> <dd><p>Receive a collection append event.</p> <p>The returned value will be used as the actual value to be appended.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.AttributeExtension.remove"> <tt class="descname">remove</tt><big>(</big><em>state</em>, <em>value</em>, <em>initiator</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.AttributeExtension.remove" title="Permalink to this definition">¶</a></dt> <dd><p>Receive a remove event.</p> <p>No return value is defined.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.orm.interfaces.AttributeExtension.set"> <tt class="descname">set</tt><big>(</big><em>state</em>, <em>value</em>, <em>oldvalue</em>, <em>initiator</em><big>)</big><a class="headerlink" href="#sqlalchemy.orm.interfaces.AttributeExtension.set" title="Permalink to this definition">¶</a></dt> <dd><p>Receive a set event.</p> <p>The returned value will be used as the actual value to be set.</p> </dd></dl> </dd></dl> </div> </div> </div> </div> <div id="docs-bottom-navigation" class="docs-navigation-links"> <div id="docs-copyright"> © <a href="../copyright.html">Copyright</a> 2007-2014, the SQLAlchemy authors and contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1. </div> </div> </div> </body> </html>