@appender needs to be a method that just accepts a mapped object to be added to 
the collection.   then, @internally_instrumented is needed to prevent the 
collections system from placing additional insturmentation on standard methods 
like __setitem__.   Also building custom dictionary collections is very 
difficult in any case (for me as well) so you were close, and that's pretty 
good !



class MyMappedCollection(dict):
    def __init__(self, data=None):
        if data:
            self.update(data)

    @collection.appender
    def _add_perm(self, pobj):
        l = dict.setdefault(self, pobj.permission, [])
        l.append(pobj)

    @collection.internally_instrumented
    def __setitem__(self, permission, pobj):
        self._add_perm(pobj)

    @collection.remover
    def _remove(self, perm):
        self[perm.permission].remove(perm)

    @collection.iterator
    def __iter__(self):
        for vals in self.itervalues():
            for perm in vals:
                yield perm



On Jun 1, 2012, at 9:38 AM, Antonio Beamud Montero wrote:

> Hi all:
> I'm trying to implement a permission system usign polymorphic associations. 
> In my example only one object has permissions associated (ULine).
> The problem is trying to use a mapping with permission names as keys, and 
> values Permission objects with a 1:1 user relation.
> A complete example here:
> 
> http://pastebin.com/SqCfBCFp
> 
> But I can't get this working, the next error is raised:
> 
>    child_state, child_dict = instance_state(child), \
> AttributeError: 'list' object has no attribute '_sa_instance_state'
> 
> A lot of thanks.
> 
> -- 
> 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.
> 

-- 
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.

Reply via email to