File tree Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Expand file tree Collapse file tree 3 files changed +36
-5
lines changed Original file line number Diff line number Diff line change 1
- from django .db import transaction
1
+ from django .db import transaction , DatabaseError
2
2
from sample_mflix .models import Movie
3
3
4
4
# start-transaction-decorator
5
5
@transaction .atomic
6
- def run_movie_transaction ():
6
+ def insert_movie_transaction ():
7
7
Movie .objects .create (
8
8
title = "Poor Things" ,
9
9
runtime = 141 ,
@@ -12,11 +12,23 @@ def run_movie_transaction():
12
12
# end-transaction-decorator
13
13
14
14
# start-transaction-manager
15
- def run_movie_transaction ():
15
+ def insert_movie_transaction ():
16
16
with transaction .atomic ():
17
17
Movie .objects .create (
18
18
title = "Poor Things" ,
19
19
runtime = 141 ,
20
20
genres = ["Comedy" , "Romance" ]
21
21
)
22
- # end-transaction-manager
22
+ # end-transaction-manager
23
+
24
+ # start-handle-errors
25
+ movie = Movie .objects .get (
26
+ title = "Jurassic Park" ,
27
+ released = timezone .make_aware (datetime (1993 , 6 , 11 ))
28
+ )
29
+ try :
30
+ with transaction .atomic ():
31
+ movie .update (title = "Jurassic Park I" )
32
+ except DatabaseError :
33
+ movie .update (title = "Jurassic Park" )
34
+ # end-handle-errors
Original file line number Diff line number Diff line change @@ -82,6 +82,9 @@ This decorator guarantees the atomicity of any database operations
82
82
within the function. If the function successfully completes, the
83
83
changes are committed MongoDB.
84
84
85
+ Examples
86
+ ~~~~~~~~
87
+
85
88
The following example calls the ``create()`` method within a transaction,
86
89
inserting a document into the ``sample_mflix.movies`` collection:
87
90
@@ -109,6 +112,22 @@ logic around your atomic code block. If you include error handing logic inside
109
112
the atomic block, {+framework+} might ignore these errors, resulting in unexpected
110
113
behavior.
111
114
115
+ If a transaction does not succeed, your application does not revert any changes made
116
+ to a model's fields. To avoid inconsistencies between your models and database documents,
117
+ you might need to manually restore the original field values.
118
+
119
+ Example
120
+ ~~~~~~~
121
+
122
+ The following example includes error handling logic that reverts the modified
123
+ ``title`` value of the retrieved document if the database transaction fails:
124
+
125
+ .. literalinclude:: /includes/interact-data/transactions.py
126
+ :start-after: start-handle-errors
127
+ :end-before: end-handle-errors
128
+ :language: python
129
+ :copyable:
130
+
112
131
Additional Information
113
132
----------------------
114
133
Original file line number Diff line number Diff line change @@ -255,7 +255,7 @@ Database and Collection Support
255
255
256
256
- ``QuerySet.union()`` is not supported within a transaction.
257
257
- If a transaction generates an error, the transaction is no longer usable.
258
- - Nested atomic blocks are not supported. The outermost atomic block starts
258
+ - Savepoints, or nested atomic blocks, are not supported. The outermost atomic block starts
259
259
a transaction, and any subsequent atomic blocks have no effect.
260
260
- Migration operations do not run inside a transaction, which differs from {+framework+}'s
261
261
default migration behavior.
You can’t perform that action at this time.
0 commit comments