the legal way is the association_proxy and family.
here another shorter ... a hack:
i did have similar need - obj.somerelation.append( left=.., right=..),
so i did monkeypatch the InstrumentedList's append to use the item
returned from collection's append -
def append( self, *args, **kwargs):
item = self._data_appender( *args,**kwargs)
self._InstrumentedList__setrecord( item)
#private __setrecord; was _before_ _data_appender
sqlalchemy.orm.attributes.InstrumentedList.append = append
and use my own collection which creates the association object from
append's keywordargs, then factory for it and give that to
collection_class= of relation()
class Association:
'base for all assoc_klas'
class MyCollection( list):
factory = None
def append( me, obj =_Relation, **kargs):
if obj is _Relation: #just marker for notset
obj = me.factory( **kargs)
list.append( me, obj)
return obj
@classmethod
def myCollectionFactory( klas):
m = Association.MyCollection()
m.factory = klas #the assoc-obj-type
return m
....
....
mapper ( ... relation( ....
collection_class = assoc_klas.myCollectionFactory
) )
---------------------------
u'll have to do it over __setitem__.
have fun.
svil
On Tuesday 12 June 2007 21:26:52 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'))
>
> The documentation has an example for using the collection_class
> argument to relation() but in order to make things actually work as
> above I think I'd need to override __setitem__ but that sqlalchemy
> doesn't seem to let me do that.
>
> What am I missing?
>
> -Ron
>
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---