views:

29

answers:

1

There is a page demonstrates how to do it when using Elixir:

http://beachcoder.wordpress.com/2007/05/02/adding-event-callbacks-to-sqlalchemyelixir-classes/

But I don't use Elixir, I just use sqlalchemy directly, and define my models as:

Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    ...

    def send_email(self):
        # send email to the user

And what I want to do is:

class User(Base):
    __tablename__ = "users"
    ...
    before_insert('init_user', 'send_email')
    before_delete('something_before_delete')

    def init_user(self):
        # init some data of the user
    def send_email(self):
        # send email to the user
    def something_before_delete(self):
        # something before delete

Note the before_insert and before_delete methods. What should I do?

+2  A: 

The answer to your question is to use an extension (MapperExtension, SessionExtension...) as described here.

But you almost certainly don't want to do anything like sending an email in such a hook. The right way would be to log the action and post-process the log entry after the transaction has completed.

avdd
@avdd, thank for your advice of 'not send email here', it's very important. And, now, I made some callbacks as the page you gave me, but there is another problem. Can you help me to: http://stackoverflow.com/questions/3646486
Freewind