Ron wrote:
>
> I have an object that has a relation to an Attributes table. The
> Attributes table is just key/value pairs with a pointer to the
> object in the main table.
>
> I'd like to make the use of my object very simple by exposing
> these object attributes as a dictionary.
>
> So, I'd like to be able to do this:
>
> obj.attrs['foo'] = 'a'
>
> instead of
>
> obj.attrs.append(Attribute('foo', 'a'))
Using the association_proxy extension in combination with your
dictionary collection class is an easy way to get this kind of
simplified access. Assuming your Attribute's value is in a
property called 'value', you can set up simple dict access like so:
class Obj(object):
attrs = association_proxy('_attrs', 'value')
mapper(Obj, ..., properties = {
'_attrs': relation(Attribute, collection_class=your_dict_class)
}
obj.attrs['foo'] = 'a'
-jek
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---