A custom list implementation for mapped relations that syncs position in a Python list with a position attribute on the mapped objects.
Prepares an OrderingList factory for use as an argument to a Mapper relation's 'collection_class' option. Arguments are:
Passes along any keyword arguments to OrderingList constructor.
A 'collection_class' list implementation that syncs position in a Python list with a position attribute on the mapped objects.
This implementation counts on the list starting in the proper order, so be SURE to put an order_by on your relation. Arguments are:
Optional. A function that maps the position in the Python list to a value to store in the ordering_attr. Values returned are usually (but need not be!) integers.
ordering_funcs are called with two positional parameters: index of the element in the list, and the list itself.
If omitted, list indexes are used for the attribute values. Two basic pre-built numbering functions are provided: 'count_from_0' and 'count_from_1'. For more exotic examples like stepped numbering, alphabetical and Fibonacci numbering, see the unit tests.
Default false. When appending an object with an existing (non-None) ordering value, that value will be left untouched unless reorder_on_append is true. This is an optimization to avoid a variety of dangerous unexpected database writes.
SQLAlchemy will add instances to the list via append() when your object loads. If for some reason the result set from the database skips a step in the ordering (say, row '1' is missing but you get '2', '3', and '4'), reorder_on_append=True would immediately renumber the items to '1', '2', '3'. If you have multiple sessions making changes, any of whom happen to load this collection even in passing, all of the sessions would try to 'clean up' the numbering in their commits, possibly causing all but one to fail with a concurrent modification error. Spooky action at a distance.
Recommend leaving this with the default of False, and just call ._reorder() if you're doing append() operations with previously ordered instances or doing housekeeping after manual sql operations.