A simple toy example:
{{{
from django.db import IntegrityError, transaction
from django.contrib.auth.models import User
def update_user(pk, username):
user = User.objects.get(pk=pk)
try:
with transaction.atomic():
user.username = username
user.is_active = False
user.save()
return user
except IntegrityError:
return user
}}}
since `username` is an unique field if an IntegrityError happen and the
transaction got reverted, the user which we return will have an
inconsistent state (if not manually reverted).
--
Ticket URL: <https://code.djangoproject.com/ticket/28479>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
The document to update is probably docs/topics/db/transactions.txt.
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:1>
* status: new => assigned
* owner: nobody => Jonatas CD
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:2>
* has_patch: 0 => 1
Comment:
Here is the [https://github.com/django/django/pull/8873 PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:3>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"c9b22707b0703db6c6ddaebdd00e2cd33d182e40" c9b22707]:
{{{
#!CommitTicketReference repository=""
revision="c9b22707b0703db6c6ddaebdd00e2cd33d182e40"
Fixed #28479 -- Doc'd that transaction rollback doesn't revert model
state.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"02210393097b0183ad2a462dbdfb900e450327eb" 0221039]:
{{{
#!CommitTicketReference repository=""
revision="02210393097b0183ad2a462dbdfb900e450327eb"
[1.11.x] Fixed #28479 -- Doc'd that transaction rollback doesn't revert
model state.
Backport of c9b22707b0703db6c6ddaebdd00e2cd33d182e40 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28479#comment:6>