This commit is contained in:
anhduy-tech
2026-01-06 23:40:27 +07:00
parent fd10218cca
commit 0f0e86b9c7
3 changed files with 15 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ from django.dispatch import receiver
from django.apps import apps from django.apps import apps
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from django.db import transaction
# Import hàm get_serializer đã có # Import hàm get_serializer đã có
from .views import get_serializer from .views import get_serializer
@@ -55,14 +56,25 @@ def generic_post_save_handler(sender, instance, created, **kwargs):
""" """
Hàm xử lý chung cho tín hiệu post_save từ BẤT KỲ model nào. Hàm xử lý chung cho tín hiệu post_save từ BẤT KỲ model nào.
""" """
change_type = "created" if created else "updated" def send_update_after_commit():
send_model_update(instance, change_type) change_type = "created" if created else "updated"
# Re-fetch the instance to ensure we have the committed data
refreshed_instance = sender.objects.get(pk=instance.pk)
send_model_update(refreshed_instance, change_type)
transaction.on_commit(send_update_after_commit)
def generic_post_delete_handler(sender, instance, **kwargs): def generic_post_delete_handler(sender, instance, **kwargs):
""" """
Hàm xử lý chung cho tín hiệu post_delete từ BẤT KỲ model nào. Hàm xử lý chung cho tín hiệu post_delete từ BẤT KỲ model nào.
""" """
send_model_update(instance, "deleted") # For delete, the action happens immediately, so on_commit is not strictly necessary
# unless the delete is part of a larger transaction that could be rolled back.
# It's safer to use it anyway.
def send_delete_after_commit():
send_model_update(instance, "deleted")
transaction.on_commit(send_delete_after_commit)
def connect_signals(): def connect_signals():
""" """