sorry, the docstring is wrong.  create_instance() should return  
EXT_CONTINUE if it would like to bypass creating the instance itself.   
However, "self" here is the MapperExtension instance, not the mapped  
instance.   the method is called before anything is created.

if you want to populate an attribute on a newly loaded instance but  
not create it, you're better off using populate_instance().  return  
EXT_CONTINUE from that method as well.

class MyExt(MapperExtension):
        def populate_instance(self, mapper, selectcontext, row, instance,  
**flags):
                if not hasattr(instance, 'mutex'):
                        instance.mutex = mutex()
                return EXT CONTINUE

the hasattr() is assuming you dont want to replace the mutex in the  
case of a session.refresh() or similar.

On Jan 30, 2008, at 1:36 PM, Ross wrote:

>
> Hello,
>
> I have recently converted a Pylons app from SQLObject to SQLAlchemy
> 0.3.10.  Conversion went quite well.
>
> I need to serialize access to some of my objects, so I've looked into
> extending MapperExtension as described at [1] to add a mutex on load.
>
> First, I define an extension and instantiate it:
> --------
> from sqlalchemy.orm import MapperExtension
> import mutex
>
> class MutexExtension(MapperExtension):
>    def create_instance(self, mapper, selectcontext, row, class_):
>        self.mutex = mutex.mutex()
>        return None
>
> mutexext = MutexExtension()
> -------
>
>
> My mapper setup looks like this:
>
> switch_mapper = mapper (Switch, switch_table,
> extension=[mutexext.mutexext, sac.ext],
>
> properties={'ports':sqla.relation(SwitchPort)})
>
>
> When I try to fetch objects from the database, I get a exception
> setting
> self.entity_name:
>
>
> Module sqlalchemy.orm.mapper:1485 in _instance
> <<                  instance = self._create_instance(context.session)
>                else:
>                    instance._entity_name = self.entity_name
>                if self.__should_log_debug:
>                    self.__log_debug("_instance(): created new
> instance %s identity %s" % (mapperutil.instance_str(instance),
> %str(identitykey)))>>
> instance._entity_name = self.entity_name
> exceptions.AttributeError: 'NoneType' object has no attribute
> '_entity_name'
>
>
> What did I do wrong?
>
> Thanks,
> Ross
>
>
>
>
> [1] 
> http://www.sqlalchemy.org/docs/03/adv_datamapping.html#advdatamapping_extending
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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