~j5-dev/+junk/sqlalchemy-0.6.1

« back to all changes in this revision

Viewing changes to doc/sqlalchemy_ext_orderinglist.html

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2009-03-25 19:25:33 UTC
  • mfrom: (1.1.18 upstream) (6.1.5 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090325192533-aju2jomjwdv1s628
Tags: 0.5.3-1
* New upstream release
* Add --install-layout=deb to setup.py call (prepare for dist-packages in
  python2.6)
  + python-all minimum required version bumped to 2.5.4-1~
* Standards-Version bumped to 3.8.1 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<html>
2
 
<head>
3
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
4
 
        <title>SQLAlchemy 0.4 Documentation - module sqlalchemy.ext.orderinglist</title>
5
 
        
6
 
    
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>
11
 
 
12
 
    <link rel="stylesheet" href="docutil.css"></link>
13
 
 
14
 
 
15
 
 
16
 
</head>
17
 
<body>
18
 
 
19
 
 
20
 
 
21
 
 
22
 
 
23
 
 
24
 
 
25
 
 
26
 
<div id="topanchor"><a name="top">&nbsp;</a></div>
27
 
 
28
 
 
29
 
<h1>SQLAlchemy 0.4 Documentation</h1>
30
 
 
31
 
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
32
 
 
33
 
<div class="versionheader">Version: 0.4.8   Last Updated: 10/12/08 13:33:19</div>
34
 
 
35
 
 
36
 
 
37
 
 
38
 
 
39
 
 
40
 
 
41
 
 
42
 
 
43
 
 
44
 
 
45
 
 
46
 
    <div class="topnav">
47
 
 
48
 
    
49
 
    <div class="navbanner">
50
 
        <a href="index.html" class="totoc">Table of Contents</a>
51
 
        
52
 
    <div class="prevnext">
53
 
            Up: <a href="docstrings.html">API Documentation</a>
54
 
 
55
 
               |   
56
 
            Previous: <a href="sqlalchemy_ext_associationproxy.html">module sqlalchemy.ext.associationproxy</a>
57
 
 
58
 
               |   
59
 
            Next: <a href="sqlalchemy_ext_sqlsoup.html">module sqlalchemy.ext.sqlsoup</a>
60
 
    </div>
61
 
 
62
 
        <h2>module sqlalchemy.ext.orderinglist</h2>
63
 
    </div>
64
 
 
65
 
        
66
 
        
67
 
    <ul>
68
 
        
69
 
        <li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_modfunc">Module Functions</a></li>
70
 
 
71
 
                <li>
72
 
                
73
 
    <ul>
74
 
        
75
 
        <li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_modfunc_ordering_list">ordering_list()</a></li>
76
 
 
77
 
    </ul>
78
 
 
79
 
                </li>
80
 
        
81
 
        <li><a style="" href="sqlalchemy_ext_orderinglist.html#docstrings_sqlalchemy.ext.orderinglist_OrderingList">class OrderingList(list)</a></li>
82
 
 
83
 
    </ul>
84
 
 
85
 
        </div>
86
 
 
87
 
 
88
 
 
89
 
    
90
 
 
91
 
    
92
 
    
93
 
    <A name="docstrings_sqlalchemy.ext.orderinglist"></a>
94
 
    
95
 
    <div class="sectionL2">
96
 
 
97
 
    <h3>module sqlalchemy.ext.orderinglist</h3>
98
 
    
99
 
    
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 &quot;position&quot; 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
111
 
 
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)))
119
 
 
120
 
class User(object): pass
121
 
class Blurb(object):
122
 
    def __init__(self, blurb):
123
 
        self.blurb = blurb
124
 
 
125
 
mapper(User, users, properties={
126
 
  'topten': relation(Blurb, collection_class=ordering_list('position'),
127
 
                     order_by=[blurbs.c.position])
128
 
})
129
 
mapper(Blurb, blurbs)
130
 
 
131
 
u = User()
132
 
u.topten.append(Blurb('Number one!'))
133
 
u.topten.append(Blurb('Number two!'))
134
 
 
135
 
# Like magic.
136
 
assert [blurb.position for blurb in u.topten] == [0, 1]
137
 
 
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.'))
141
 
 
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
145
 
</pre>
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
148
 
examples.</p>
149
 
<p>The <b>ordering_list</b> function is the ORM-compatible
150
 
constructor for OrderingList instances.</p>
151
 
</div>
152
 
    
153
 
 
154
 
        
155
 
    
156
 
    <A name="docstrings_sqlalchemy.ext.orderinglist_modfunc"></a>
157
 
    
158
 
    <div class="sectionL3">
159
 
 
160
 
    <h3>Module Functions</h3>
161
 
    
162
 
    
163
 
                
164
 
    <div class="darkcell">
165
 
    
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">
173
 
<dt>attr</dt>
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>
181
 
</dl>
182
 
<p>Passes along any keyword arguments to <tt class="docutils literal"><span class="pre">OrderingList</span></tt> constructor.</p>
183
 
 
184
 
    </div>
185
 
    </div>
186
 
 
187
 
        
188
 
 
189
 
    </div>
190
 
 
191
 
 
192
 
 
193
 
 
194
 
            
195
 
    
196
 
 
197
 
    
198
 
    
199
 
    <A name="docstrings_sqlalchemy.ext.orderinglist_OrderingList"></a>
200
 
    
201
 
    <div class="sectionL3">
202
 
 
203
 
    <h3>class OrderingList(list)</h3>
204
 
    
205
 
    
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>
210
 
</div>
211
 
    
212
 
 
213
 
                    
214
 
    <div class="darkcell">
215
 
    
216
 
    <A name=""></a>
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
222
 
mapped objects.</p>
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
238
 
the unit tests.</p>
239
 
</dd>
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 &quot;clean up&quot; 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>
258
 
</dd>
259
 
</dl>
260
 
 
261
 
    </div>
262
 
    </div>
263
 
 
264
 
                    
265
 
    <div class="darkcell">
266
 
    
267
 
    <A name=""></a>
268
 
    <b>def append(<i>self</i>, <i>entity</i>)</b>
269
 
    <div class="docstring">
270
 
    
271
 
    </div>
272
 
    </div>
273
 
 
274
 
                    
275
 
    <div class="darkcell">
276
 
    
277
 
    <A name=""></a>
278
 
    <b>def insert(<i>self</i>, <i>index</i>, <i>entity</i>)</b>
279
 
    <div class="docstring">
280
 
    
281
 
    </div>
282
 
    </div>
283
 
 
284
 
                    
285
 
    <div class="darkcell">
286
 
    
287
 
    <A name=""></a>
288
 
    <b>def pop(<i>self</i>, <i>index=-1</i>)</b>
289
 
    <div class="docstring">
290
 
    
291
 
    </div>
292
 
    </div>
293
 
 
294
 
                    
295
 
    <div class="darkcell">
296
 
    
297
 
    <A name=""></a>
298
 
    <b>def remove(<i>self</i>, <i>entity</i>)</b>
299
 
    <div class="docstring">
300
 
    
301
 
    </div>
302
 
    </div>
303
 
 
304
 
                    
305
 
    <div class="darkcell">
306
 
    
307
 
    <A name=""></a>
308
 
    <b>def __delitem__(<i>self</i>, <i>index</i>)</b>
309
 
    <div class="docstring">
310
 
    
311
 
    </div>
312
 
    </div>
313
 
 
314
 
                    
315
 
    <div class="darkcell">
316
 
    
317
 
    <A name=""></a>
318
 
    <b>def __delslice__(<i>self</i>, <i>start</i>, <i>end</i>)</b>
319
 
    <div class="docstring">
320
 
    
321
 
    </div>
322
 
    </div>
323
 
 
324
 
                    
325
 
    <div class="darkcell">
326
 
    
327
 
    <A name=""></a>
328
 
    <b>def __setitem__(<i>self</i>, <i>index</i>, <i>entity</i>)</b>
329
 
    <div class="docstring">
330
 
    
331
 
    </div>
332
 
    </div>
333
 
 
334
 
                    
335
 
    <div class="darkcell">
336
 
    
337
 
    <A name=""></a>
338
 
    <b>def __setslice__(<i>self</i>, <i>start</i>, <i>end</i>, <i>values</i>)</b>
339
 
    <div class="docstring">
340
 
    
341
 
    </div>
342
 
    </div>
343
 
 
344
 
 
345
 
    
346
 
 
347
 
            <a href="#top" class="totoc">back to section top</a>
348
 
    </div>
349
 
 
350
 
 
351
 
 
352
 
    
353
 
 
354
 
    </div>
355
 
 
356
 
 
357
 
 
358
 
 
359
 
 
360
 
    <div class="bottomnav">
361
 
        
362
 
    <div class="prevnext">
363
 
            Up: <a href="docstrings.html">API Documentation</a>
364
 
 
365
 
               |   
366
 
            Previous: <a href="sqlalchemy_ext_associationproxy.html">module sqlalchemy.ext.associationproxy</a>
367
 
 
368
 
               |   
369
 
            Next: <a href="sqlalchemy_ext_sqlsoup.html">module sqlalchemy.ext.sqlsoup</a>
370
 
    </div>
371
 
 
372
 
    </div>
373
 
 
374
 
 
375
 
 
376
 
 
377
 
 
378
 
 
379
 
 
380
 
 
381
 
</body>
382
 
</html>
383
 
 
384
 
 
385
 
 
386
 
 
387
 
 
388