3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
4
<title>SQLAlchemy 0.4 Documentation - module sqlalchemy.ext.orderinglist</title>
7
<link rel="stylesheet" href="style.css"></link>
8
<link rel="stylesheet" href="docs.css"></link>
9
<link href="syntaxhighlight.css" rel="stylesheet" type="text/css"></link>
10
<script src="scripts.js"></script>
12
<link rel="stylesheet" href="docutil.css"></link>
26
<div id="topanchor"><a name="top"> </a></div>
29
<h1>SQLAlchemy 0.4 Documentation</h1>
31
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
33
<div class="versionheader">Version: 0.4.8 Last Updated: 10/12/08 13:33:19</div>
49
<div class="navbanner">
50
<a href="index.html" class="totoc">Table of Contents</a>
52
<div class="prevnext">
53
Up: <a href="docstrings.html">API Documentation</a>
56
Previous: <a href="sqlalchemy_ext_associationproxy.html">module sqlalchemy.ext.associationproxy</a>
59
Next: <a href="sqlalchemy_ext_sqlsoup.html">module sqlalchemy.ext.sqlsoup</a>
62
<h2>module sqlalchemy.ext.orderinglist</h2>
69
<li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_modfunc">Module Functions</a></li>
75
<li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_modfunc_ordering_list">ordering_list()</a></li>
81
<li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_OrderingList">class OrderingList(list)</a></li>
93
<A name="docstrings_sqlalchemy.ext.orderinglist"></a>
95
<div class="sectionL2">
97
<h3>module sqlalchemy.ext.orderinglist</h3>
100
<div class="darkcell"><p>A custom list that manages index/position information for its children.</p>
101
<p><tt class="docutils literal"><span class="pre">orderinglist</span></tt> is a custom list collection implementation for mapped relations
102
that keeps an arbitrary "position" attribute on contained objects in sync with
103
each object's position in the Python list.</p>
104
<p>The collection acts just like a normal Python <tt class="docutils literal"><span class="pre">list</span></tt>, with the added
105
behavior that as you manipulate the list (via <tt class="docutils literal"><span class="pre">insert</span></tt>, <tt class="docutils literal"><span class="pre">pop</span></tt>, assignment,
106
deletion, what have you), each of the objects it contains is updated as needed
107
to reflect its position. This is very useful for managing ordered relations
108
which have a user-defined, serialized order:</p>
109
<pre class="literal-block">
110
from sqlalchemy.ext.orderinglist import ordering_list
112
users = Table('users', metadata,
113
Column('id', Integer, primary_key=True))
114
blurbs = Table('user_top_ten_list', metadata,
115
Column('id', Integer, primary_key=True),
116
Column('user_id', Integer, ForeignKey('users.id')),
117
Column('position', Integer),
118
Column('blurb', String(80)))
120
class User(object): pass
122
def __init__(self, blurb):
125
mapper(User, users, properties={
126
'topten': relation(Blurb, collection_class=ordering_list('position'),
127
order_by=[blurbs.c.position])
129
mapper(Blurb, blurbs)
132
u.topten.append(Blurb('Number one!'))
133
u.topten.append(Blurb('Number two!'))
136
assert [blurb.position for blurb in u.topten] == [0, 1]
138
# The objects will be renumbered automaticaly after any list-changing
139
# operation, for example an insert:
140
u.topten.insert(1, Blurb('I am the new Number Two.'))
142
assert [blurb.position for blurb in u.topten] == [0, 1, 2]
143
assert u.topten[1].blurb == 'I am the new Number Two.'
144
assert u.topten[1].position == 1
146
<p>Numbering and serialization are both highly configurable. See the docstrings
147
in this module and the main SQLAlchemy documentation for more information and
149
<p>The <b>ordering_list</b> function is the ORM-compatible
150
constructor for OrderingList instances.</p>
156
<A name="docstrings_sqlalchemy.ext.orderinglist_modfunc"></a>
158
<div class="sectionL3">
160
<h3>Module Functions</h3>
164
<div class="darkcell">
166
<A name="docstrings_sqlalchemy.ext.orderinglist_modfunc_ordering_list"></a>
167
<b>def ordering_list(<i>attr</i>, <i>count_from=None</i>, <i>**kw</i>)</b>
168
<div class="docstring">
169
<p>Prepares an OrderingList factory for use in mapper definitions.</p>
170
<p>Returns an object suitable for use as an argument to a Mapper relation's
171
<tt class="docutils literal"><span class="pre">collection_class</span></tt> option. Arguments are:</p>
172
<dl class="docutils">
174
<dd>Name of the mapped attribute to use for storage and retrieval of
175
ordering information</dd>
176
<dt>count_from (optional)</dt>
177
<dd>Set up an integer-based ordering, starting at <tt class="docutils literal"><span class="pre">count_from</span></tt>. For
178
example, <tt class="docutils literal"><span class="pre">ordering_list('pos',</span> <span class="pre">count_from=1)</span></tt> would create a 1-based
179
list in SQL, storing the value in the 'pos' column. Ignored if
180
<tt class="docutils literal"><span class="pre">ordering_func</span></tt> is supplied.</dd>
182
<p>Passes along any keyword arguments to <tt class="docutils literal"><span class="pre">OrderingList</span></tt> constructor.</p>
199
<A name="docstrings_sqlalchemy.ext.orderinglist_OrderingList"></a>
201
<div class="sectionL3">
203
<h3>class OrderingList(list)</h3>
206
<div class="darkcell"><p>A custom list that manages position information for its children.</p>
207
<p>See the module and __init__ documentation for more details. The
208
<tt class="docutils literal"><span class="pre">ordering_list</span></tt> function is used to configure <tt class="docutils literal"><span class="pre">OrderingList</span></tt>
209
collections in <tt class="docutils literal"><span class="pre">mapper</span></tt> relation definitions.</p>
214
<div class="darkcell">
217
<b>def __init__(<i>self</i>, <i>ordering_attr=None</i>, <i>ordering_func=None</i>, <i>reorder_on_append=False</i>)</b>
218
<div class="docstring">
219
<p>A custom list that manages position information for its children.</p>
220
<p><tt class="docutils literal"><span class="pre">OrderingList</span></tt> is a <tt class="docutils literal"><span class="pre">collection_class</span></tt> list implementation that
221
syncs position in a Python list with a position attribute on the
223
<p>This implementation relies on the list starting in the proper order,
224
so be <strong>sure</strong> to put an <tt class="docutils literal"><span class="pre">order_by</span></tt> on your relation.</p>
225
<dl class="docutils">
226
<dt>ordering_attr</dt>
227
<dd>Name of the attribute that stores the object's order in the relation.</dd>
228
<dt>ordering_func</dt>
229
<dd><p class="first">Optional. A function that maps the position in the Python list to a
230
value to store in the <tt class="docutils literal"><span class="pre">ordering_attr</span></tt>. Values returned are usually
231
(but need not be!) integers.</p>
232
<p>An <tt class="docutils literal"><span class="pre">ordering_func</span></tt> is called with two positional parameters: the
233
index of the element in the list, and the list itself.</p>
234
<p class="last">If omitted, Python list indexes are used for the attribute values.
235
Two basic pre-built numbering functions are provided in this module:
236
<tt class="docutils literal"><span class="pre">count_from_0</span></tt> and <tt class="docutils literal"><span class="pre">count_from_1</span></tt>. For more exotic examples
237
like stepped numbering, alphabetical and Fibonacci numbering, see
240
<dt>reorder_on_append</dt>
241
<dd><p class="first">Default False. When appending an object with an existing (non-None)
242
ordering value, that value will be left untouched unless
243
<tt class="docutils literal"><span class="pre">reorder_on_append</span></tt> is true. This is an optimization to avoid a
244
variety of dangerous unexpected database writes.</p>
245
<p>SQLAlchemy will add instances to the list via append() when your
246
object loads. If for some reason the result set from the database
247
skips a step in the ordering (say, row '1' is missing but you get
248
'2', '3', and '4'), reorder_on_append=True would immediately
249
renumber the items to '1', '2', '3'. If you have multiple sessions
250
making changes, any of whom happen to load this collection even in
251
passing, all of the sessions would try to "clean up" the numbering
252
in their commits, possibly causing all but one to fail with a
253
concurrent modification error. Spooky action at a distance.</p>
254
<p class="last">Recommend leaving this with the default of False, and just call
255
<tt class="docutils literal"><span class="pre">_reorder()</span></tt> if you're doing <tt class="docutils literal"><span class="pre">append()</span></tt> operations with
256
previously ordered instances or when doing some housekeeping after
257
manual sql operations.</p>
265
<div class="darkcell">
268
<b>def append(<i>self</i>, <i>entity</i>)</b>
269
<div class="docstring">
275
<div class="darkcell">
278
<b>def insert(<i>self</i>, <i>index</i>, <i>entity</i>)</b>
279
<div class="docstring">
285
<div class="darkcell">
288
<b>def pop(<i>self</i>, <i>index=-1</i>)</b>
289
<div class="docstring">
295
<div class="darkcell">
298
<b>def remove(<i>self</i>, <i>entity</i>)</b>
299
<div class="docstring">
305
<div class="darkcell">
308
<b>def __delitem__(<i>self</i>, <i>index</i>)</b>
309
<div class="docstring">
315
<div class="darkcell">
318
<b>def __delslice__(<i>self</i>, <i>start</i>, <i>end</i>)</b>
319
<div class="docstring">
325
<div class="darkcell">
328
<b>def __setitem__(<i>self</i>, <i>index</i>, <i>entity</i>)</b>
329
<div class="docstring">
335
<div class="darkcell">
338
<b>def __setslice__(<i>self</i>, <i>start</i>, <i>end</i>, <i>values</i>)</b>
339
<div class="docstring">
347
<a href="#top" class="totoc">back to section top</a>
360
<div class="bottomnav">
362
<div class="prevnext">
363
Up: <a href="docstrings.html">API Documentation</a>
366
Previous: <a href="sqlalchemy_ext_associationproxy.html">module sqlalchemy.ext.associationproxy</a>
369
Next: <a href="sqlalchemy_ext_sqlsoup.html">module sqlalchemy.ext.sqlsoup</a>