db.transaction.clean_savepoints()

clean_savepoints(using=None) [source]

Resets the counter used to generate unique savepoint IDs.

The following example demonstrates the use of savepoints:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.db import transaction
 
# open a transaction
@transaction.atomic
def viewfunc(request):
 
    a.save()
    # transaction now contains a.save()
 
    sid = transaction.savepoint()
 
    b.save()
    # transaction now contains a.save() and b.save()
 
    if want_to_keep_b:
        transaction.savepoint_commit(sid)
        # open transaction still contains a.save() and b.save()
    else:
        transaction.savepoint_rollback(sid)
        # open transaction now contains only a.save()

Savepoints may be used to recover from a database error by performing a partial rollback. If you’re doing this inside an atomic() block, the entire block will still be rolled back, because it doesn’t know you’ve handled the situation at a lower level! To prevent this, you can control the rollback behavior with the following functions.

doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.