2👍
First of whole, I guess you need to learn Savepoints well, as describe in the docs.
The reason your changes has been applied is that s.save() is implemented before raising exception. by s.save() you add a savepoint and by Django’s default behavior in atomic transactions (you can read the codes of django.db.transaction.Atomic, specially the def __exit__(self):
part) it commits the last savepoint existing in a transaction.
You can find more details and examples in the docs, like how you can roll back to a specific savepoint and more details about savepoints, rollback and commit.
Explanation about ATOMIC_REQUESTS:
Django’s default behavior is to set ATOMIC_REQUESTS to False. By setting ATOMIC_REQUESTS to True, you will add the following changes. By docs:
It works like this. Before calling a view function, Django starts a transaction. If the response is produced without problems, Django commits the transaction. If the view produces an exception, Django rolls back the transaction.
It means you will change the default behavior like this, and Django warns you about it:
While the simplicity of this transaction model is appealing, it also makes it inefficient when traffic increases. Opening a transaction for every view has some overhead. The impact on performance depends on the query patterns of your application and on how well your database handles locking.
It warns you to use this option carefully, because by setting this, each of your view calls will be in a single transaction and still you should be careful about performance (e.g. caring about table and row locks, caring about external api calls) and implementation (e.g. this link) difficulties you will face.
By the way, you can add this functionality to a view or function by @transaction.atomic
decorator, or some lines of code using with transaction.atomic():
statement. This makes you use atomic transactions just when you need it.
I strongly recommend you to read this section.
1👍
It’s not 'ATOMIC_REQUEST'
in your code below. It’s 'ATOMIC_REQUESTS'
DATABASES = {
'default': {
# ...
'ATOMIC_REQUEST': True,
# ↑ "S" is missing.
}
}
So, this is the correct one below:
DATABASES = {
'default': {
# ...
'ATOMIC_REQUESTS': True,
# ↑ "S" is added.
}
}
- [Django]-Writing Testcases for google cloud endpoints API written on top of django
- [Django]-Django-translation: How to translate languages
- [Django]-Get current user in Django admin
- [Django]-Why do you need to use Django REST API?