1
是否可以告诉SQLAlchemy为relationship
存储使用OrderedDict
?我只熟悉attribute_mapped_collection
,但这是无序的。使用OrderedDict进行SQLAlchemy关系()?
是否可以告诉SQLAlchemy为relationship
存储使用OrderedDict
?我只熟悉attribute_mapped_collection
,但这是无序的。使用OrderedDict进行SQLAlchemy关系()?
有这样的文档的example:
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)
在这里,我想通过我的所有文档的阅读,一定是错过了这个...谢谢男人,并为麻烦抱歉 –