Ron wrote:
>
> So now I if I try to get something from the dict:
>
> obj.attr['foo']
>
> I get this error:
>
> KeyError: <schema.Attribute object at 0xb78a8e0c>
Hard to say without a stacktrace or looking at the code, but maybe
this will be useful:
from sqlalchemy import *
from sqlalchemy.ext.associationproxy import association_proxy
e = create_engine('sqlite:///')
m = BoundMetaData(e)
parents = Table('parents', m,
Column('id', Integer, primary_key=True),
Column('stuff', String))
attributes = Table('attributes', m,
Column('id', Integer, primary_key=True),
Column('parent_id', Integer,
ForeignKey('parents.id')),
Column('key', String),
Column('value', String))
class AttributeDictNEW(dict):
def append(self, value):
self[value.key] = value
def __iter__(self):
return self.itervalues()
class Parent(object):
attrs = association_proxy('_attrs', 'value')
class Attribute(object):
def __init__(self, key, value):
self.key = key
self.value = value
mapper(Attribute, attributes)
mapper(Parent, parents, properties={
'_attrs': relation(Attribute,
collection_class=AttributeDictNEW)
})
m.create_all()
p = Parent()
p.attrs['foo'] = 'a'
print p.attrs['foo']
session = create_session()
session.save(p)
session.flush()
pid = p.id
session.clear()
p = session.query(Parent).get(pid)
assert 'foo' in p.attrs
assert p.attrs['foo'] == 'a'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---