3

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?

1 Answer 1

5

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.

[EDIT] The sqlalchemy docs have changed; the new way of handling this is to use ORM Events

Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/3646486

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.