Moved the database transaction control from the save() method in the data model to the form view, to avoid double transactions.
This commit is contained in:
parent
04703df6b5
commit
926d61f534
@ -178,17 +178,16 @@ class Transaction(DirtyFieldsMixin, BaseModel):
|
|||||||
for record in to_save:
|
for record in to_save:
|
||||||
record.current_user = self.current_user
|
record.current_user = self.current_user
|
||||||
# Runs the update
|
# Runs the update
|
||||||
with transaction.atomic():
|
super().save(force_insert=force_insert, force_update=force_update,
|
||||||
super().save(force_insert=force_insert, force_update=force_update,
|
using=using, update_fields=update_fields)
|
||||||
using=using, update_fields=update_fields)
|
for record in to_delete:
|
||||||
for record in to_delete:
|
record.delete()
|
||||||
record.delete()
|
for record in to_save:
|
||||||
for record in to_save:
|
record.save(force_insert=force_insert,
|
||||||
record.save(force_insert=force_insert,
|
force_update=force_update,
|
||||||
force_update=force_update,
|
using=using, update_fields=update_fields)
|
||||||
using=using, update_fields=update_fields)
|
for x in txn_to_sort:
|
||||||
for x in txn_to_sort:
|
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
||||||
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
|
||||||
|
|
||||||
def delete(self, using=None, keep_parents=False):
|
def delete(self, using=None, keep_parents=False):
|
||||||
txn_same_day = list(
|
txn_same_day = list(
|
||||||
@ -199,12 +198,10 @@ class Transaction(DirtyFieldsMixin, BaseModel):
|
|||||||
for i in range(len(txn_same_day)):
|
for i in range(len(txn_same_day)):
|
||||||
if txn_same_day[i].ord != i + 1:
|
if txn_same_day[i].ord != i + 1:
|
||||||
txn_to_sort.append([txn_same_day[i], i + 1])
|
txn_to_sort.append([txn_same_day[i], i + 1])
|
||||||
with transaction.atomic():
|
Record.objects.filter(transaction=self).delete()
|
||||||
for record in self.record_set.all():
|
super().delete(using=using, keep_parents=keep_parents)
|
||||||
record.delete()
|
for x in txn_to_sort:
|
||||||
super().delete(using=using, keep_parents=keep_parents)
|
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
||||||
for x in txn_to_sort:
|
|
||||||
Transaction.objects.filter(pk=x[0].pk).update(ord=x[1])
|
|
||||||
|
|
||||||
def fill_from_post(self, post: Dict[str, str], request: HttpRequest,
|
def fill_from_post(self, post: Dict[str, str], request: HttpRequest,
|
||||||
txn_type: str):
|
txn_type: str):
|
||||||
|
@ -24,6 +24,7 @@ from dirtyfields import DirtyFieldsMixin
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
|
from django.db import transaction
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
from django.http import HttpResponse, HttpRequest, \
|
from django.http import HttpResponse, HttpRequest, \
|
||||||
HttpResponseRedirect, Http404
|
HttpResponseRedirect, Http404
|
||||||
@ -157,7 +158,8 @@ class FormView(View):
|
|||||||
and not self.object.is_dirty(check_relationship=True):
|
and not self.object.is_dirty(check_relationship=True):
|
||||||
message = self.get_not_modified_message(form.cleaned_data)
|
message = self.get_not_modified_message(form.cleaned_data)
|
||||||
else:
|
else:
|
||||||
self.object.save()
|
with transaction.atomic():
|
||||||
|
self.object.save()
|
||||||
message = self.get_success_message(form.cleaned_data)
|
message = self.get_success_message(form.cleaned_data)
|
||||||
messages.success(self.request, message)
|
messages.success(self.request, message)
|
||||||
return redirect(str(UrlBuilder(self.get_success_url())
|
return redirect(str(UrlBuilder(self.get_success_url())
|
||||||
|
Loading…
Reference in New Issue
Block a user