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]
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
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]