diff --git a/api/__pycache__/settings.cpython-313.pyc b/api/__pycache__/settings.cpython-313.pyc index 18384023..8cb90ed5 100644 Binary files a/api/__pycache__/settings.cpython-313.pyc and b/api/__pycache__/settings.cpython-313.pyc differ diff --git a/app/__pycache__/signals.cpython-313.pyc b/app/__pycache__/signals.cpython-313.pyc index 79a17894..c6fd9c2d 100644 Binary files a/app/__pycache__/signals.cpython-313.pyc and b/app/__pycache__/signals.cpython-313.pyc differ diff --git a/app/signals.py b/app/signals.py index fe7b1a32..3e4bb6f6 100644 --- a/app/signals.py +++ b/app/signals.py @@ -3,6 +3,7 @@ from django.dispatch import receiver from django.apps import apps from channels.layers import get_channel_layer from asgiref.sync import async_to_sync +from django.db import transaction # Import hàm get_serializer đã có 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. """ - change_type = "created" if created else "updated" - send_model_update(instance, change_type) + def send_update_after_commit(): + 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): """ 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(): """