From 3b81b3ab7ce779fe688c13aeedf32adee3df25d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Abac=C4=B1?= Date: Fri, 28 May 2021 12:19:34 +0300 Subject: [PATCH] Add transaction.on_commit to send_email task --- django_ses_plus/models.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/django_ses_plus/models.py b/django_ses_plus/models.py index e63af36..a6c1c77 100644 --- a/django_ses_plus/models.py +++ b/django_ses_plus/models.py @@ -2,7 +2,7 @@ from django.conf import settings from django.contrib.auth import get_user_model -from django.db import models +from django.db import models, transaction from django.template.loader import render_to_string from django.utils import translation from django.utils.translation import ugettext_lazy as _ @@ -51,12 +51,14 @@ def send_email(self, subject, template_path, context, from_email=None, language= translation.activate(language) html_message = render_to_string(template_path, context) - send_email.delay( - subject=subject, - to_email=self.get_to_email(), - html_message=html_message, - from_email=from_email, - recipient_id=recipient_id + transaction.on_commit( + lambda: send_email.delay( + subject=subject, + to_email=self.get_to_email(), + html_message=html_message, + from_email=from_email, + recipient_id=recipient_id + ) ) if language: