Python 是否将OrderedDict用于SQLAlchemy关系()?
可以告诉SQLAlchemy使用OrderedDict进行关系存储吗?我只熟悉属性映射集合,但这是无序的。文档中有这样一个例子:Python 是否将OrderedDict用于SQLAlchemy关系()?,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,可以告诉SQLAlchemy使用OrderedDict进行关系存储吗?我只熟悉属性映射集合,但这是无序的。文档中有这样一个例子: from sqlalchemy.util import OrderedDict from sqlalchemy.orm.collections import MappedCollection class NodeMap(OrderedDict, MappedCollection): """Holds 'Node' objects, keyed by the
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)
在这里我想我读了所有的文件,一定错过了这个。。。谢谢你,伙计,很抱歉给你添麻烦