On Oct 4, 2010, at 8:26 PM, Ritesh Nadhani wrote:
> Hello
>
> This has been making me pull my hair for quite sometime now :)
>
> I am trying to use MapperExtension and I have a code like:
> http://bpaste.net/show/9936/
>
> Basically, I share the model across two apps, so I want one with the
> extension and one without. Thus the need to use one of the internal
> methods:
>
> __mapper__,exception.push(....)
>
> Unfortunately, it the whole thing works when I use, __mapper_args__
> but does not if I add it dynamically using the above method. Though,
> other methods like before_update, row_translate get called in both
> ways of setup. Just not reconstruct_instance, which I what I wanted.
>
> I will now startup a debugger but I was wondering if anybody knew
> about it or its a bug or its just not possible?
It's not a bug since ad-hoc addition of extensions is not public API. The
mapper checks its extensions for this particular hook when its created and sets
an extra event listener on the class if present.
0.7 will allow ad-hoc listeners in a more flexible way.
There is actually a "listener" interface for this in 0.6 which is semi-private.
It will be changing a little bit in 0.7 (whereas MapperExtension will remain
backwards compatible for at least through 0.7/0.8). If you wanted to try that
its:
from sqlalchemy.orm.attributes import manager_of_class
def reconstruct(instance):
print "reconstruct!"
manager_of_class(SomeTable).events.add_listener('on_load', reconstruct)
In this case its likely much easier to set up __mapper_args__ as a callable on
your class instead (see the examples in declarative regarding mixins - the same
technique works on the class itself), or to use the @reconstructor decorator
inside a conditional:
class MyClass(Base):
if my_app_config_foo_bar:
@reconstructor
def reconstruct(self):
print "reconstruct!"
--
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.