Use OrderedDict for SQLAlchemy relationship()?
<p>Is it possible to tell SQLAlchemy to use <code>OrderedDict</code> for the <code>relationship</code> storage? I'm only familiar with <code>attribute_mapped_collection</code>, but that's unordered.</p>

<p>There's an <a href="http://docs.sqlalchemy.org/en/latest/orm/collections.html#custom-dictionary-based-collections" rel="nofollow">example</a> of this in the docs:</p>

<pre><code>from sqlalchemy.util import OrderedDict
from sqlalchemy.orm.collections import MappedCollection

class NodeMap(OrderedDict, MappedCollection):
    """Holds 'Node' objects, keyed by the 'name' attribute with insert order maintained."""

    def __init__(self, *args, **kw):
        MappedCollection.__init__(self, keyfunc=lambda node: node.name)
        OrderedDict.__init__(self, *args, **kw)
</code></pre>

<p>Usage is simply:</p>

<pre><code>foo = relationship(..., collection_class=NodeMap)
</code></pre>

python python-3.x sqlalchemy