object save needs to be atomic transaction so entire object is saved
This commit is contained in:
parent
7a2854317e
commit
7c5ad70781
1 changed files with 23 additions and 22 deletions
|
@ -4,7 +4,7 @@ from collections import OrderedDict
|
||||||
from functools import reduce, wraps
|
from functools import reduce, wraps
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models, transaction
|
||||||
from django.db.models import Field, FieldDoesNotExist, Manager, ManyToManyRel, Prefetch, Q
|
from django.db.models import Field, FieldDoesNotExist, Manager, ManyToManyRel, Prefetch, Q
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
|
@ -276,31 +276,32 @@ class ModelInstanceWrapper(BaseWrapper):
|
||||||
author = self._author
|
author = self._author
|
||||||
if self.pk is None:
|
if self.pk is None:
|
||||||
self._changeset.add_create(self, author=author)
|
self._changeset.add_create(self, author=author)
|
||||||
for field, initial_value in self._initial_values.items():
|
with transaction.atomic():
|
||||||
if field.many_to_one:
|
for field, initial_value in self._initial_values.items():
|
||||||
try:
|
if field.many_to_one:
|
||||||
new_value = getattr(self._obj, field.get_cache_name())
|
try:
|
||||||
except AttributeError:
|
new_value = getattr(self._obj, field.get_cache_name())
|
||||||
new_value = getattr(self._obj, field.attname)
|
except AttributeError:
|
||||||
else:
|
new_value = getattr(self._obj, field.attname)
|
||||||
new_value = None if new_value is None else new_value.pk
|
else:
|
||||||
|
new_value = None if new_value is None else new_value.pk
|
||||||
|
|
||||||
if new_value != initial_value:
|
if new_value != initial_value:
|
||||||
self._changeset.add_update(self, name=field.name, value=new_value, author=author)
|
self._changeset.add_update(self, name=field.name, value=new_value, author=author)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
new_value = getattr(self._obj, field.name)
|
new_value = getattr(self._obj, field.name)
|
||||||
if new_value == initial_value:
|
if new_value == initial_value:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if field.name == 'titles':
|
if field.name == 'titles':
|
||||||
for lang in (set(initial_value.keys()) | set(new_value.keys())):
|
for lang in (set(initial_value.keys()) | set(new_value.keys())):
|
||||||
new_title = new_value.get(lang, '')
|
new_title = new_value.get(lang, '')
|
||||||
if new_title != initial_value.get(lang, ''):
|
if new_title != initial_value.get(lang, ''):
|
||||||
self._changeset.add_update(self, name='title_'+lang, value=new_title, author=author)
|
self._changeset.add_update(self, name='title_'+lang, value=new_title, author=author)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._changeset.add_update(self, name=field.name, value=field.get_prep_value(new_value), author=author)
|
self._changeset.add_update(self, name=field.name, value=field.get_prep_value(new_value), author=author)
|
||||||
|
|
||||||
def delete(self, author=None):
|
def delete(self, author=None):
|
||||||
if author is None:
|
if author is None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue