1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
<!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>
Events
—
SQLAlchemy 0.9 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/sphinx_paramlinks.css" type="text/css" />
<link rel="stylesheet" href="../_static/changelog.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.9.8',
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.9 Documentation" href="../index.html" />
<link rel="up" title="SQLAlchemy Core" href="index.html" />
<link rel="next" title="Core Events" href="events.html" />
<link rel="prev" title="Connection Pooling" href="pooling.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.9.8</span> | Release Date: October 13, 2014
</div>
<h1>SQLAlchemy 0.9 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.9 Documentation</a></h3>
<p id="sidebar-paginate">
<a href="index.html" title="SQLAlchemy Core">Up</a> |
<a href="pooling.html" title="Connection Pooling">Prev</a> |
<a href="events.html" title="Core Events">Next</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="#">
Events
</a></h3>
<ul>
<li><a class="reference internal" href="#">Events</a><ul>
<li><a class="reference internal" href="#event-registration">Event Registration</a></li>
<li><a class="reference internal" href="#named-argument-styles">Named Argument Styles</a></li>
<li><a class="reference internal" href="#targets">Targets</a></li>
<li><a class="reference internal" href="#modifiers">Modifiers</a></li>
<li><a class="reference internal" href="#event-reference">Event Reference</a></li>
<li><a class="reference internal" href="#api-reference">API Reference</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="docs-body" class="withsidebar" >
<div class="section" id="events">
<span id="event-toplevel"></span><h1>Events<a class="headerlink" href="#events" title="Permalink to this headline">¶</a></h1>
<p>SQLAlchemy includes an event API which publishes a wide variety of hooks into
the internals of both SQLAlchemy Core and ORM.</p>
<div class="versionadded">
<p><span>New in version 0.7: </span>The system supersedes the previous system of “extension”, “proxy”,
and “listener” classes.</p>
</div>
<div class="section" id="event-registration">
<h2>Event Registration<a class="headerlink" href="#event-registration" title="Permalink to this headline">¶</a></h2>
<p>Subscribing to an event occurs through a single API point, the <a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a> function,
or alternatively the <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">listens_for()</span></tt></a> decorator. These functions
accept a user-defined listening function, a string identifier which identifies the event to be
intercepted, and a target. Additional positional and keyword arguments to these
two functions may be supported by
specific types of events, which may specify alternate interfaces for the given event function, or provide
instructions regarding secondary event targets based on the given target.</p>
<p>The name of an event and the argument signature of a corresponding listener function is derived from
a class bound specification method, which exists bound to a marker class that’s described in the documentation.
For example, the documentation for <a class="reference internal" href="events.html#sqlalchemy.events.PoolEvents.connect" title="sqlalchemy.events.PoolEvents.connect"><tt class="xref py py-meth docutils literal"><span class="pre">PoolEvents.connect()</span></tt></a> indicates that the event name is <tt class="docutils literal"><span class="pre">"connect"</span></tt>
and that a user-defined listener function should receive two positional arguments:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.event</span> <span class="kn">import</span> <span class="n">listen</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.pool</span> <span class="kn">import</span> <span class="n">Pool</span>
<span class="k">def</span> <span class="nf">my_on_connect</span><span class="p">(</span><span class="n">dbapi_con</span><span class="p">,</span> <span class="n">connection_record</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"New DBAPI connection:"</span><span class="p">,</span> <span class="n">dbapi_con</span>
<span class="n">listen</span><span class="p">(</span><span class="n">Pool</span><span class="p">,</span> <span class="s">'connect'</span><span class="p">,</span> <span class="n">my_on_connect</span><span class="p">)</span></pre></div>
</div>
<p>To listen with the <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">listens_for()</span></tt></a> decorator looks like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.event</span> <span class="kn">import</span> <span class="n">listens_for</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.pool</span> <span class="kn">import</span> <span class="n">Pool</span>
<span class="nd">@listens_for</span><span class="p">(</span><span class="n">Pool</span><span class="p">,</span> <span class="s">"connect"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_on_connect</span><span class="p">(</span><span class="n">dbapi_con</span><span class="p">,</span> <span class="n">connection_record</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"New DBAPI connection:"</span><span class="p">,</span> <span class="n">dbapi_con</span></pre></div>
</div>
</div>
<div class="section" id="named-argument-styles">
<h2>Named Argument Styles<a class="headerlink" href="#named-argument-styles" title="Permalink to this headline">¶</a></h2>
<p>There are some varieties of argument styles which can be accepted by listener
functions. Taking the example of <a class="reference internal" href="events.html#sqlalchemy.events.PoolEvents.connect" title="sqlalchemy.events.PoolEvents.connect"><tt class="xref py py-meth docutils literal"><span class="pre">PoolEvents.connect()</span></tt></a>, this function
is documented as receiving <tt class="docutils literal"><span class="pre">dbapi_connection</span></tt> and <tt class="docutils literal"><span class="pre">connection_record</span></tt> arguments.
We can opt to receive these arguments by name, by establishing a listener function
that accepts <tt class="docutils literal"><span class="pre">**keyword</span></tt> arguments, by passing <tt class="docutils literal"><span class="pre">named=True</span></tt> to either
<a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a> or <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">listens_for()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.event</span> <span class="kn">import</span> <span class="n">listens_for</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.pool</span> <span class="kn">import</span> <span class="n">Pool</span>
<span class="nd">@listens_for</span><span class="p">(</span><span class="n">Pool</span><span class="p">,</span> <span class="s">"connect"</span><span class="p">,</span> <span class="n">named</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_on_connect</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"New DBAPI connection:"</span><span class="p">,</span> <span class="n">kw</span><span class="p">[</span><span class="s">'dbapi_connection'</span><span class="p">])</span></pre></div>
</div>
<p>When using named argument passing, the names listed in the function argument
specification will be used as keys in the dictionary.</p>
<p>Named style passes all arguments by name regardless of the function
signature, so specific arguments may be listed as well, in any order,
as long as the names match up:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.event</span> <span class="kn">import</span> <span class="n">listens_for</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.pool</span> <span class="kn">import</span> <span class="n">Pool</span>
<span class="nd">@listens_for</span><span class="p">(</span><span class="n">Pool</span><span class="p">,</span> <span class="s">"connect"</span><span class="p">,</span> <span class="n">named</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_on_connect</span><span class="p">(</span><span class="n">dbapi_connection</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">"New DBAPI connection:"</span><span class="p">,</span> <span class="n">dbapi_connection</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Connection record:"</span><span class="p">,</span> <span class="n">kw</span><span class="p">[</span><span class="s">'connection_record'</span><span class="p">])</span></pre></div>
</div>
<p>Above, the presence of <tt class="docutils literal"><span class="pre">**kw</span></tt> tells <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">listens_for()</span></tt></a> that
arguments should be passed to the function by name, rather than positionally.</p>
<div class="versionadded">
<p><span>New in version 0.9.0: </span>Added optional <tt class="docutils literal"><span class="pre">named</span></tt> argument dispatch to
event calling.</p>
</div>
</div>
<div class="section" id="targets">
<h2>Targets<a class="headerlink" href="#targets" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a> function is very flexible regarding targets. It
generally accepts classes, instances of those classes, and related
classes or objects from which the appropriate target can be derived.
For example, the above mentioned <tt class="docutils literal"><span class="pre">"connect"</span></tt> event accepts
<a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> classes and objects as well as <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt></a> classes
and objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.event</span> <span class="kn">import</span> <span class="n">listen</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.pool</span> <span class="kn">import</span> <span class="n">Pool</span><span class="p">,</span> <span class="n">QueuePool</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">Engine</span>
<span class="kn">import</span> <span class="nn">psycopg2</span>
<span class="k">def</span> <span class="nf">connect</span><span class="p">():</span>
<span class="k">return</span> <span class="n">psycopg2</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s">'ed'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'127.0.0.1'</span><span class="p">,</span> <span class="n">dbname</span><span class="o">=</span><span class="s">'test'</span><span class="p">)</span>
<span class="n">my_pool</span> <span class="o">=</span> <span class="n">QueuePool</span><span class="p">(</span><span class="n">connect</span><span class="p">)</span>
<span class="n">my_engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">'postgresql://ed@localhost/test'</span><span class="p">)</span>
<span class="c"># associate listener with all instances of Pool</span>
<span class="n">listen</span><span class="p">(</span><span class="n">Pool</span><span class="p">,</span> <span class="s">'connect'</span><span class="p">,</span> <span class="n">my_on_connect</span><span class="p">)</span>
<span class="c"># associate listener with all instances of Pool</span>
<span class="c"># via the Engine class</span>
<span class="n">listen</span><span class="p">(</span><span class="n">Engine</span><span class="p">,</span> <span class="s">'connect'</span><span class="p">,</span> <span class="n">my_on_connect</span><span class="p">)</span>
<span class="c"># associate listener with my_pool</span>
<span class="n">listen</span><span class="p">(</span><span class="n">my_pool</span><span class="p">,</span> <span class="s">'connect'</span><span class="p">,</span> <span class="n">my_on_connect</span><span class="p">)</span>
<span class="c"># associate listener with my_engine.pool</span>
<span class="n">listen</span><span class="p">(</span><span class="n">my_engine</span><span class="p">,</span> <span class="s">'connect'</span><span class="p">,</span> <span class="n">my_on_connect</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="modifiers">
<h2>Modifiers<a class="headerlink" href="#modifiers" title="Permalink to this headline">¶</a></h2>
<p>Some listeners allow modifiers to be passed to <a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a>. These
modifiers sometimes provide alternate calling signatures for
listeners. Such as with ORM events, some event listeners can have a
return value which modifies the subsequent handling. By default, no
listener ever requires a return value, but by passing <tt class="docutils literal"><span class="pre">retval=True</span></tt>
this value can be supported:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">validate_phone</span><span class="p">(</span><span class="n">target</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="sd">"""Strip non-numeric characters from a phone number"""</span>
<span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r'(?![0-9])'</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="c"># setup listener on UserContact.phone attribute, instructing</span>
<span class="c"># it to use the return value</span>
<span class="n">listen</span><span class="p">(</span><span class="n">UserContact</span><span class="o">.</span><span class="n">phone</span><span class="p">,</span> <span class="s">'set'</span><span class="p">,</span> <span class="n">validate_phone</span><span class="p">,</span> <span class="n">retval</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="event-reference">
<h2>Event Reference<a class="headerlink" href="#event-reference" title="Permalink to this headline">¶</a></h2>
<p>Both SQLAlchemy Core and SQLAlchemy ORM feature a wide variety of event hooks:</p>
<ul class="simple">
<li><strong>Core Events</strong> - these are described in
<a class="reference internal" href="events.html"><em>Core Events</em></a> and include event hooks specific to
connection pool lifecycle, SQL statement execution,
transaction lifecycle, and schema creation and teardown.</li>
<li><strong>ORM Events</strong> - these are described in
<a class="reference internal" href="../orm/events.html"><em>ORM Events</em></a>, and include event hooks specific to
class and attribute instrumentation, object initialization
hooks, attribute on-change hooks, session state, flush, and
commit hooks, mapper initialization, object/result population,
and per-instance persistence hooks.</li>
</ul>
</div>
<div class="section" id="api-reference">
<h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="sqlalchemy.event.listen">
<tt class="descclassname">sqlalchemy.event.</tt><tt class="descname">listen</tt><big>(</big><em>target</em>, <em>identifier</em>, <em>fn</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.event.listen" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a listener function for the given target.</p>
<p>e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">event</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.schema</span> <span class="kn">import</span> <span class="n">UniqueConstraint</span>
<span class="k">def</span> <span class="nf">unique_constraint_name</span><span class="p">(</span><span class="n">const</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
<span class="n">const</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">"uq_</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span>
<span class="n">table</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="nb">list</span><span class="p">(</span><span class="n">const</span><span class="o">.</span><span class="n">columns</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
<span class="p">)</span>
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
<span class="n">UniqueConstraint</span><span class="p">,</span>
<span class="s">"after_parent_attach"</span><span class="p">,</span>
<span class="n">unique_constraint_name</span><span class="p">)</span></pre></div>
</div>
<p>A given function can also be invoked for only the first invocation
of the event using the <tt class="docutils literal"><span class="pre">once</span></tt> argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">on_config</span><span class="p">():</span>
<span class="n">do_config</span><span class="p">()</span>
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="n">Mapper</span><span class="p">,</span> <span class="s">"before_configure"</span><span class="p">,</span> <span class="n">on_config</span><span class="p">,</span> <span class="n">once</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<div class="versionadded">
<p><span>New in version 0.9.4: </span>Added <tt class="docutils literal"><span class="pre">once=True</span></tt> to <a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a>
and <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">event.listens_for()</span></tt></a>.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="sqlalchemy.event.listens_for">
<tt class="descclassname">sqlalchemy.event.</tt><tt class="descname">listens_for</tt><big>(</big><em>target</em>, <em>identifier</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.event.listens_for" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorate a function as a listener for the given target + identifier.</p>
<p>e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">event</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.schema</span> <span class="kn">import</span> <span class="n">UniqueConstraint</span>
<span class="nd">@event.listens_for</span><span class="p">(</span><span class="n">UniqueConstraint</span><span class="p">,</span> <span class="s">"after_parent_attach"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">unique_constraint_name</span><span class="p">(</span><span class="n">const</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
<span class="n">const</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">"uq_</span><span class="si">%s</span><span class="s">_</span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span>
<span class="n">table</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="nb">list</span><span class="p">(</span><span class="n">const</span><span class="o">.</span><span class="n">columns</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span>
<span class="p">)</span></pre></div>
</div>
<p>A given function can also be invoked for only the first invocation
of the event using the <tt class="docutils literal"><span class="pre">once</span></tt> argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@event.listens_for</span><span class="p">(</span><span class="n">Mapper</span><span class="p">,</span> <span class="s">"before_configure"</span><span class="p">,</span> <span class="n">once</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_config</span><span class="p">():</span>
<span class="n">do_config</span><span class="p">()</span></pre></div>
</div>
<div class="versionadded">
<p><span>New in version 0.9.4: </span>Added <tt class="docutils literal"><span class="pre">once=True</span></tt> to <a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a>
and <a class="reference internal" href="#sqlalchemy.event.listens_for" title="sqlalchemy.event.listens_for"><tt class="xref py py-func docutils literal"><span class="pre">event.listens_for()</span></tt></a>.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="sqlalchemy.event.remove">
<tt class="descclassname">sqlalchemy.event.</tt><tt class="descname">remove</tt><big>(</big><em>target</em>, <em>identifier</em>, <em>fn</em><big>)</big><a class="headerlink" href="#sqlalchemy.event.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove an event listener.</p>
<p>The arguments here should match exactly those which were sent to
<a class="reference internal" href="#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a>; all the event registration which proceeded as a result
of this call will be reverted by calling <a class="reference internal" href="#sqlalchemy.event.remove" title="sqlalchemy.event.remove"><tt class="xref py py-func docutils literal"><span class="pre">remove()</span></tt></a> with the same
arguments.</p>
<p>e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># if a function was registered like this...</span>
<span class="nd">@event.listens_for</span><span class="p">(</span><span class="n">SomeMappedClass</span><span class="p">,</span> <span class="s">"before_insert"</span><span class="p">,</span> <span class="n">propagate</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_listener_function</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
<span class="k">pass</span>
<span class="c"># ... it's removed like this</span>
<span class="n">event</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">SomeMappedClass</span><span class="p">,</span> <span class="s">"before_insert"</span><span class="p">,</span> <span class="n">my_listener_function</span><span class="p">)</span></pre></div>
</div>
<p>Above, the listener function associated with <tt class="docutils literal"><span class="pre">SomeMappedClass</span></tt> was also
propagated to subclasses of <tt class="docutils literal"><span class="pre">SomeMappedClass</span></tt>; the <a class="reference internal" href="#sqlalchemy.event.remove" title="sqlalchemy.event.remove"><tt class="xref py py-func docutils literal"><span class="pre">remove()</span></tt></a>
function will revert all of these operations.</p>
<div class="versionadded">
<p><span>New in version 0.9.0.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="sqlalchemy.event.contains">
<tt class="descclassname">sqlalchemy.event.</tt><tt class="descname">contains</tt><big>(</big><em>target</em>, <em>identifier</em>, <em>fn</em><big>)</big><a class="headerlink" href="#sqlalchemy.event.contains" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if the given target/ident/fn is set up to listen.</p>
<div class="versionadded">
<p><span>New in version 0.9.0.</span></p>
</div>
</dd></dl>
</div>
</div>
</div>
</div>
<div id="docs-bottom-navigation" class="docs-navigation-links">
Previous:
<a href="pooling.html" title="previous chapter">Connection Pooling</a>
Next:
<a href="events.html" title="next chapter">Core Events</a>
<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>
|