59๐
โ
Use an atomic transaction:
Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.
Examples:
from django.db import transaction
with transaction.atomic():
model1.save()
model2.save()
and
from django.db import transaction, IntegrityError
try:
with transaction.atomic():
model1.save()
model2.save()
except IntegrityError:
handle_exception()
๐คLeistungsabfall
3๐
Alternative solution using a decorator:
from django.db import transaction
@transaction.atomic
def myview(request):
# This code executes inside a transaction.
model1.save()
model2.save()
๐คMarcel
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-What is the best way to access stored procedures in Django's ORM
Source:stackexchange.com