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
22from sample_mflix .models import Movie
33
44# start-transaction-decorator
55@transaction .atomic
6- def run_movie_transaction ():
6+ def insert_movie_transaction ():
77 Movie .objects .create (
88 title = "Poor Things" ,
99 runtime = 141 ,
@@ -12,11 +12,23 @@ def run_movie_transaction():
1212# end-transaction-decorator
1313
1414# start-transaction-manager
15- def run_movie_transaction ():
15+ def insert_movie_transaction ():
1616 with transaction .atomic ():
1717 Movie .objects .create (
1818 title = "Poor Things" ,
1919 runtime = 141 ,
2020 genres = ["Comedy" , "Romance" ]
2121 )
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
8282within the function. If the function successfully completes, the
8383changes are committed MongoDB.
8484
85+ Examples
86+ ~~~~~~~~
87+
8588The following example calls the ``create()`` method within a transaction,
8689inserting a document into the ``sample_mflix.movies`` collection:
8790
@@ -109,6 +112,22 @@ logic around your atomic code block. If you include error handing logic inside
109112the atomic block, {+framework+} might ignore these errors, resulting in unexpected
110113behavior.
111114
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+
112131Additional Information
113132----------------------
114133
Original file line number Diff line number Diff line change @@ -255,7 +255,7 @@ Database and Collection Support
255255
256256 - ``QuerySet.union()`` is not supported within a transaction.
257257 - 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
259259 a transaction, and any subsequent atomic blocks have no effect.
260260 - Migration operations do not run inside a transaction, which differs from {+framework+}'s
261261 default migration behavior.
You can’t perform that action at this time.
0 commit comments