Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to define your own collection class. There are only 3 methods to implement: appender, remover, and converter. See <code>sqlalchemy.orm.collections.MappedCollection</code> as an example.</p> <p><strong>Update:</strong> Here is quick-n-dirty implementation according to your requirements:</p> <pre><code>from sqlalchemy import * from sqlalchemy.orm import mapper, relation, sessionmaker from sqlalchemy.orm.collections import collection metadata = MetaData() parent = Table('parent', metadata, Column('parent_id', Integer, primary_key=True), Column('name', Unicode), ) child = Table('child', metadata, Column('child_id', Integer, primary_key=True), Column('name', Unicode), ) parent_child = Table('parent_child', metadata, Column('parent_id', Integer, ForeignKey(parent.c.parent_id)), Column('child_id', Integer, ForeignKey(child.c.child_id)), Column('number', Integer), PrimaryKeyConstraint('parent_id', 'child_id'), ) class ParentChild(object): def __init__(self, child, number): self.child = child self.number = number class Parent(object): pass class Child(object): pass class MyMappedCollection(object): def __init__(self, data=None): self._data = data or {} @collection.appender def _append(self, parent_child): l = self._data.setdefault(parent_child.number, []) l.append(parent_child) def __setitem__(self, number, child): self._append(ParentChild(number=number, child=child)) def __getitem__(self, number): return tuple(pc.child for pc in self._data[number]) @collection.remover def _remove(self, parent_child): self._data[parent_child.number].remove(parent_child) @collection.iterator def _iterator(self): for pcs in self._data.itervalues(): for pc in pcs: yield pc def __repr__(self): return '%s(%r)' % (type(self).__name__, self._data) mapper(Parent, parent, properties={ 'children': relation(ParentChild, collection_class=MyMappedCollection), }) mapper(Child, child) mapper(ParentChild, parent_child, properties={ 'parent': relation(Parent), 'child': relation(Child), }) engine = create_engine('sqlite://') db = sessionmaker(bind=engine)() metadata.create_all(bind=engine) p = Parent() c1 = Child() c2 = Child() c3 = Child() p.children[1] = c1 p.children[1] = c2 p.children[2] = c3 db.add(p) db.commit() p_id = p.parent_id db.expunge_all() p = db.query(Parent).get(p_id) print p.children[1] print p.children[2] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload