post_save
signal is the cleanest option among these three.
Just eliminate if kwargs['created']
and call do_something()
right in the body of a function:
def model_created_or_updated(sender, **kwargs):
the_instance = kwargs['instance']
do_something()
post_save.connect(model_created_or_updated, sender=YourModel)
Now, every time YourModel
is created or updated do_something()
would be called.
FYI, created
is just a flag that indicates whether YourModel
was created (value is True
) or updated (value is False
).
Note that do_something()
will be executed synchronously and will be "blocking" until done. If you want to execute do_something
asynchronously, you need to go with a real task management tool, like celery
package.