Fixed transaction handling problem if a race condition occurs during invoice creation

This commit is contained in:
Raphael Michel
2016-09-05 23:19:25 +02:00
parent 30ab315fec
commit f1495f242f

View File

@@ -2,7 +2,7 @@ import string
from datetime import date
from decimal import Decimal
from django.db import DatabaseError, models
from django.db import DatabaseError, models, transaction
from django.db.models import Max
from django.utils.crypto import get_random_string
from django.utils.functional import cached_property
@@ -88,7 +88,8 @@ class Invoice(models.Model):
else:
self.invoice_no = self._get_invoice_number_from_order()
try:
return super().save(*args, **kwargs)
with transaction.atomic():
return super().save(*args, **kwargs)
except DatabaseError:
# Suppress duplicate key errors and try again
if i == 9: