signals vs. save()

7 views
Skip to first unread message

Robert

unread,
Jun 15, 2007, 10:53:14 AM6/15/07
to Django users
Hi,

What is a benefit of using django signals instead of modifying the
save() method ?

Reading some code, I've noticed that many use counters columns in
Models,
and update with post_save() signal instead of doing this in save()
method ?
I understand them as it avoids of doing counts all the time (if not
using cache).

Example:
nr of posts posts in forum thread, scores.. etc.

When should I use post_save() and when should I use save() ?

Thanks,

--
Robert

Malcolm Tredinnick

unread,
Jun 15, 2007, 6:15:15 PM6/15/07
to django...@googlegroups.com
On Fri, 2007-06-15 at 14:53 +0000, Robert wrote:
> Hi,
>
> What is a benefit of using django signals instead of modifying the
> save() method ?
>
> Reading some code, I've noticed that many use counters columns in
> Models,
> and update with post_save() signal instead of doing this in save()
> method ?
> I understand them as it avoids of doing counts all the time (if not
> using cache).

For many cases, there's no benefit at all and using save() is by far the
most understandle way to code something. However, a save() method
applies to a particular model class. If you want to implement some
functionality that operates across multiple types of models, you need to
use the signal infrastructure, since the same signal is raised no matter
what the type of model (you can differentiate the model type in the
signal handler, though).

So for model-specific stuff, use save(). For something that is intended
to act across multiple model types, use signals.

Regards,
Malcolm


Reply all
Reply to author
Forward
0 new messages