简体   繁体   English

使用OrderedDict进行SQLAlchemy关系()?

[英]Use OrderedDict for SQLAlchemy relationship()?

Is it possible to tell SQLAlchemy to use OrderedDict for the relationship storage? 是否有可能告诉SQLAlchemy使用OrderedDict进行relationship存储? I'm only familiar with attribute_mapped_collection , but that's unordered. 我只熟悉attribute_mapped_collection ,但那是无序的。

There's an example of this in the docs: 在文档中有一个这样的例子

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)

Usage is simply: 用法很简单:

foo = relationship(..., collection_class=NodeMap)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM