Signal Decorator¶
In Django, developer usually use post_save signal to perform certain actions after a model instance is saved.
Even created parameter indicates whether the instance is newly created or an existing one, this is not that straightforward.
With turbo_helper, we provide syntax suger to make it more clear, just like Rails.
from turbo_helper import after_create_commit, after_update_commit, after_delete_commit
@after_create_commit(sender=Message)
def create_message_content(sender, instance, created, **kwargs):
broadcast_action_to(
"chat",
instance.chat_id,
action="append",
template="demo_openai/message_content.html",
context={
"instance": instance,
},
target=dom_id(instance.chat_id, "message_list"),
)
Notes:
after_create_commit,after_update_commit,after_delete_commit, are decorators, they are used to decorate a function, which will be called after the model instance is created, updated or deleted.The function decorated by
after_create_commit,after_update_commit, receive the same arguments aspost_savesignal handler.The function decorated by
after_delete_commitreceive the same arguments aspost_deletesignal handler.This can make our code more clear, especially when we need to some broadcasts.
django-lifecycle¶
Another approach is to use django-lifecycle package, which is inspired by Rails’ ActiveRecord callbacks.
So we can write the code in Django model like this:
@hook(AFTER_UPDATE, on_commit=True)
def broadcast_updated(self):
pass
@hook(BEFORE_DELETE)
def broadcast_deleted(self):
pass