On Sat, Apr 26, 2008 at 7:48 PM, Yann Cointepas <[EMAIL PROTECTED]> wrote:
>
>  Hi,
>
>  I noticed that __init__ method of classes derived from Entity is not
>  called when querying database (I inculded a sample code below). Is
>  there a way to call a user defined __init__ (or eventually another
>  method) upon instances creation triggered by a query ?
>
>  In the following code, Movie.__init__ is not called by
>  Movie.query.all():
>
>  from elixir import *
>
>  metadata.bind = "sqlite:///movies.sqlite"
>  #metadata.bind.echo = True
>
>  class Movie(Entity):
>     title = Field(Unicode(30))
>     year = Field(Integer)
>     description = Field(Text)
>
>     def __init__( self, *args, **kwargs ):
>       print 'Movie.__init__', args, kwargs
>       super( Movie, self ).__init__( *args, **kwargs )
>
>
>     def __repr__(self):
>         return '<Movie "%s" (%d)>' % (self.title, self.year)
>
>
>
>  print 'Create database'
>  setup_all()
>  create_all()
>  Movie(title=u"Blade Runner", year=1982)
>  session.flush()
>
>  print 'Query'
>  print Movie.query.all()

See SQLAlchemy FAQ:
http://www.sqlalchemy.org/trac/wiki/FAQ#whyisntmy__init__calledwhenIloadobjects

The solution in Elixir terms would translate to something like:

class MyExt(MapperExtension):
    def create_instance(self, mapper, selectcontext, row, class_):
        return MyObject()

class MyObject(Entity):
    [...]
    using_mapper_options(extension=MyExt())

-- 
Gaƫtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to