All pastes #519135 Raw Edit

Use a dictionary!

public python v1 · immutable
#519135 ·published 2007-05-29 15:34 UTC
rendered paste body
from elixir import metadata, objectstore, Entity, using_mapper_optionsfrom elixir import has_field, belongs_to, has_many, Unicodefrom sqlalchemy import MapperExtension, EXT_PASSclass TestMapperExtension(MapperExtension):        def append_result(self, mapper, selectcontext, row, instance, identitykey, result, isnew):                instance._magic_method()             return EXT_PASS        class MyDict(dict):    def append(self, item):        self[item.text] = item    def __iter__(self):        return iter(self.values())    class Test(Entity):        has_field('text', Unicode(255))    has_many('data', of_kind='TestData',collection_class=MyDict)    #using_mapper_options(extension=TestMapperExtension()) #COMMENTING AND UNCOMMENTING THIS LINE OUTLINES THE PROBLEM        def _magic_method(self):        print self.text        for item in self.data:            print " ", item.text                        class TestData(Entity):        has_field('text', Unicode(255))    belongs_to('parent', of_kind='Test')    metadata.connect('sqlite:///:memory:')metadata.create_all()    t = Test(text="a", data=[TestData(text="b"), TestData(text="c")])objectstore.flush()t._magic_method()del tobjectstore.clear()t = Test.select()[0]t._magic_method()print "accessing from dict like this: t.data['c'].text"print t.data['c'].text