Signals for User model

3 views
Skip to first unread message

Aidas Bendoraitis

unread,
Mar 7, 2007, 5:58:19 AM3/7/07
to django...@googlegroups.com
I've never used signals before and I think I came to a point when I
need to start doing that.

What I need is to call some function just after the User is saved.

According to the docs that I found about signals, I should do something like:

# --- code --- #
from django.contrib.auth.models import User
from django.dispatch import dispatcher

def my_function_to_call(sender, instance, signal, *args, **kwargs):
pass

dispatcher.connect(my_function_to_call, signal=signals.post_save, sender=User)

# --- /code --- #

Where should I put this code, so that I wouldn't need to modify the
contributed models and the code was read and executed when the user is
saved?

Regards,
Aidas Bendoraitis [aka Archatas]

Malcolm Tredinnick

unread,
Mar 7, 2007, 6:10:44 AM3/7/07
to django...@googlegroups.com

You can put the code anywhere you like, providing it will be executed.
One thing to do might be to put an "import signal_hookups" in your
settings.py file and put the dispatch connection in signal.hookups.py.

Or put it in one of your model's files, if you are going to be using
that model in the process of handling the signal.

Basically, you just need to put the dispatch call somewhere it will be
executed. The my_function_to_call() function can be anywhere you like;
just make sure you import it wherever you set up the dispatch.

Regards,
Malcolm


Aidas Bendoraitis

unread,
Mar 7, 2007, 9:54:31 AM3/7/07
to django...@googlegroups.com
Thanks! I put the function just in the file of one of the models (it
is used for handling the signal) and it worked perfectly.

I just came to an idea that the pre_init and post_init signals could
be used to extend contributed models without modifying their files.
The question that cames to my mind is: How to attach an attribute,
property or method to an existing Python class? Any examples or
directions?

Regards,
Aidas Bendoraitis [aka Archatas]

Reply all
Reply to author
Forward
0 new messages