是否可以告诉SQLAlchemy将OrderedDict用于relationship存储?我只熟悉attribute_mapped_collection,但这是无序的。
发布于 2016-10-04 18:56:07
在文档中有这样的一个示例:
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)用法很简单:
foo = relationship(..., collection_class=NodeMap)https://stackoverflow.com/questions/39857194
复制相似问题