Thanks - populate_instance does exactly what I was looking for!

I went to SA 0.3 with SAContext because I was familiar with it.
Porting from SQLObject was straightforward, but a fairly large task.
>From reading at pylonshq, converting 0.3 to 0.4 seemed to be much
easier than converting SQLObject to SQLAlchemy.  So I decided I'd
tackle them as two phases so I could keep my code working for as long
as possible.

I know I'm going to want to upgrade at some point because I'll make
good use of the self-referential updates.

Thanks for the tips!

On Jan 30, 2:48 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> oh, 0.3.  well theres another question.  If you just converted from
> SQLObject, why to SA 0.3 ?  0.4 is the currently supported version and
> is also vastly superior to 0.3.
>
> On Jan 30, 2008, at 2:40 PM, jason kirtland wrote:
>
>
>
> > On 0.3.x, use EXT_PASS rather than EXT_CONTINUE.
>
> > Michael Bayer wrote:
> >> 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...
--~--~---------~--~----~------------~-------~--~----~
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