From 073c2711d8d6b59d5319a54ce1579cf08b051bd4 Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 10 Jul 2025 10:55:20 -0400 Subject: [PATCH 01/13] DOCSP-51036: Transactions --- source/interact-data.txt | 4 ++++ source/interact-data/transactions.txt | 28 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 source/interact-data/transactions.txt diff --git a/source/interact-data.txt b/source/interact-data.txt index c2bc78f..66be919 100644 --- a/source/interact-data.txt +++ b/source/interact-data.txt @@ -24,6 +24,7 @@ Interact with Data CRUD Operations Specify a Query Perform Raw Queries + Transactions In this section, you can learn how to use {+django-odm+} to interact with your MongoDB data. @@ -36,3 +37,6 @@ MongoDB data. - :ref:`django-raw-queries`: Learn how to use MongoDB's aggregation pipeline syntax or the PyMongo driver to query your data. + +- :ref:`django-transactions`: Learn how to use {+framework+}'s transactions API + to run data operations within a transaction. diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt new file mode 100644 index 0000000..04104ff --- /dev/null +++ b/source/interact-data/transactions.txt @@ -0,0 +1,28 @@ +.. _django-transactions: + +========================= +Transactions and Sessions +========================= + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: code example, ACID compliance, multi-document + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +Overview +-------- + +In this guide, you can learn how to use the {+django-odm+} to perform +**transactions**. Transactions allow you to perform a series of operations +that change data only if the entire transaction is committed. +If any operation in the transaction does not succeed, the library stops the +transaction and discards all data changes before they ever become +visible. This feature is called **atomicity**. \ No newline at end of file From 07a82acc6a70721f015610a0249c20eaa31bd05c Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 10 Jul 2025 16:24:11 -0400 Subject: [PATCH 02/13] work in progress --- source/includes/interact-data/transactions.py | 22 +++++ source/interact-data/transactions.txt | 96 ++++++++++++++++++- source/limitations-upcoming.txt | 12 ++- 3 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 source/includes/interact-data/transactions.py diff --git a/source/includes/interact-data/transactions.py b/source/includes/interact-data/transactions.py new file mode 100644 index 0000000..a64d56c --- /dev/null +++ b/source/includes/interact-data/transactions.py @@ -0,0 +1,22 @@ +from django.db import transaction +from sample_mflix.models import Movie + +# start-transaction-decorator +@transaction.atomic +def run_movie_transaction(): + Movie.objects.create( + title="Poor Things", + runtime=141, + genres=["Comedy", "Romance"] + ) +# end-transaction-decorator + +# start-transaction-manager +def run_movie_transaction(): + with transaction.atomic(): + Movie.objects.create( + title="Poor Things", + runtime=141, + genres=["Comedy", "Romance"] + ) +# end-transaction-manager \ No newline at end of file diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 04104ff..14f0534 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -20,9 +20,97 @@ Transactions and Sessions Overview -------- -In this guide, you can learn how to use the {+django-odm+} to perform -**transactions**. Transactions allow you to perform a series of operations +In this guide, you can learn how to use {+django-odm+} to perform +**transactions**. Transactions allow you to run a series of operations that change data only if the entire transaction is committed. -If any operation in the transaction does not succeed, the library stops the +If any operation in the transaction does not succeed, {+django-odm+} stops the transaction and discards all data changes before they ever become -visible. This feature is called **atomicity**. \ No newline at end of file +visible. This feature is called **atomicity**. + +In MongoDB, transactions run within logical sessions. A +session is a grouping of related read or write operations that you +want to run sequentially. Sessions enable causal consistency for a group +of operations and allow you to run operations in an **ACID-compliant** +transaction, which is a transaction that meets an expectation of +atomicity, consistency, isolation, and durability. + +You can use {+framework+}'s transaction API to perform database transactions. +To run operations within a transaction, define them inside an atomic block of +code. {+framework+} manages session logic internally, so you do not need to +manually start a session before running a transaction. + +.. important:: Transaction Limitations + + {+django-odm+}'s support for the {+framework+} transaction API + has several limitations. To view a list of limitations, see + :ref:`Database and Collection Support ` + in the Django and MongoDB Feature Compatibility guide. + +Sample Data +~~~~~~~~~~~ + +The examples in this guide use the ``Movie`` model, which represents +the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets `. +The ``Movie`` model class has the following definition: + +.. literalinclude:: /includes/interact-data/crud.py + :start-after: start-models + :end-before: end-models + :language: python + :copyable: + +.. include:: /includes/use-sample-data.rst + + .. replacement:: model-classes + + ``Movie`` model includes + + .. replacement:: model-imports + + .. code-block:: python + + from .models import Movie + from django.utils import timezone + from datetime import datetime + +Start a Transaction +------------------- + +To start a database transaction, define an atomic block of code +by adding the ``@transaction.atomic`` decorator above your function. +This decorator guarantees the atomicity of any database operations +within the function. If the function successfully completes, the +changes are committed MongoDB. + +The following example calls the ``create()`` method within a transaction, +inserting a document into the ``sample_mflix.movies`` collection: + +.. literalinclude:: /includes/interact-data/transactions.py + :start-after: start-transaction-decorator + :end-before: end-transaction-decorator + :language: python + :copyable: + +Alternatively, you can use the ``transaction.atomic()`` context manager +to create an atomic block. This example runs the same operation as the +previous example but uses a context manager to start a transaction: + +.. literalinclude:: /includes/interact-data/transactions.py + :start-after: start-transaction-manager + :end-before: end-transaction-manager + :language: python + :copyable: + +Handle Transaction Errors +------------------------- + +To handle exceptions that occur during a transaction, add error handling +logic around your atomic code block. If you include error handing logic inside +the atomic block, {+framework+} might ignore these errors, resulting in unexpected +behavior. + +Additional Information +---------------------- + +To learn more about the {+framework+} transaction API, see `Database Transactions +<{+django-docs+}/topics/db/transactions>`__ in the {+framework+} documentation. \ No newline at end of file diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index 2b3510a..c893ab3 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -183,6 +183,8 @@ Query Support the :ref:`raw_aggregate() method `. - ✓ +.. _django-feature-compat-db-coll: + Database and Collection Support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -248,7 +250,15 @@ Database and Collection Support - ✓ * - Transactions - - *Unsupported*. + - *Partially Supported*. You can use {+framework+}'s transactions API with the + following limitations: + + - ``QuerySet.union()`` is not supported within a transaction. + - If a transaction generates an error, the transaction is no longer usable. + - Nested atomic blocks are not supported. The outermost atomic block starts + a transaction, and any subsequent atomic blocks have no effect. + - Migration operations do not run inside a transaction, which differs from {+framework+}'s + default migration behavior. - ✓ Django Features From f1560c96a251e01b82261ad1b9b0254056b5a1f3 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 10:57:16 -0400 Subject: [PATCH 03/13] edits --- source/includes/interact-data/transactions.py | 20 +++++++++++++++---- source/interact-data/transactions.txt | 19 ++++++++++++++++++ source/limitations-upcoming.txt | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/source/includes/interact-data/transactions.py b/source/includes/interact-data/transactions.py index a64d56c..281ba20 100644 --- a/source/includes/interact-data/transactions.py +++ b/source/includes/interact-data/transactions.py @@ -1,9 +1,9 @@ -from django.db import transaction +from django.db import transaction, DatabaseError from sample_mflix.models import Movie # start-transaction-decorator @transaction.atomic -def run_movie_transaction(): +def insert_movie_transaction(): Movie.objects.create( title="Poor Things", runtime=141, @@ -12,11 +12,23 @@ def run_movie_transaction(): # end-transaction-decorator # start-transaction-manager -def run_movie_transaction(): +def insert_movie_transaction(): with transaction.atomic(): Movie.objects.create( title="Poor Things", runtime=141, genres=["Comedy", "Romance"] ) -# end-transaction-manager \ No newline at end of file +# end-transaction-manager + +# start-handle-errors +movie = Movie.objects.get( + title="Jurassic Park", + released=timezone.make_aware(datetime(1993, 6, 11)) +) +try: + with transaction.atomic(): + movie.update(title="Jurassic Park I") +except DatabaseError: + movie.update(title="Jurassic Park") +# end-handle-errors \ No newline at end of file diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 14f0534..fe1b449 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -82,6 +82,9 @@ This decorator guarantees the atomicity of any database operations within the function. If the function successfully completes, the changes are committed MongoDB. +Examples +~~~~~~~~ + The following example calls the ``create()`` method within a transaction, inserting a document into the ``sample_mflix.movies`` collection: @@ -109,6 +112,22 @@ logic around your atomic code block. If you include error handing logic inside the atomic block, {+framework+} might ignore these errors, resulting in unexpected behavior. +If a transaction does not succeed, your application does not revert any changes made +to a model's fields. To avoid inconsistencies between your models and database documents, +you might need to manually restore the original field values. + +Example +~~~~~~~ + +The following example includes error handling logic that reverts the modified +``title`` value of the retrieved document if the database transaction fails: + +.. literalinclude:: /includes/interact-data/transactions.py + :start-after: start-handle-errors + :end-before: end-handle-errors + :language: python + :copyable: + Additional Information ---------------------- diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index c893ab3..79dce3c 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -255,7 +255,7 @@ Database and Collection Support - ``QuerySet.union()`` is not supported within a transaction. - If a transaction generates an error, the transaction is no longer usable. - - Nested atomic blocks are not supported. The outermost atomic block starts + - Savepoints, or nested atomic blocks, are not supported. The outermost atomic block starts a transaction, and any subsequent atomic blocks have no effect. - Migration operations do not run inside a transaction, which differs from {+framework+}'s default migration behavior. From 005217fd2804bda44042cdcfd5706c2de968e7e9 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 10:59:21 -0400 Subject: [PATCH 04/13] atlas search idx note --- source/interact-data/raw-queries.txt | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/source/interact-data/raw-queries.txt b/source/interact-data/raw-queries.txt index 7498340..d481b90 100644 --- a/source/interact-data/raw-queries.txt +++ b/source/interact-data/raw-queries.txt @@ -164,19 +164,10 @@ that covers the fields you want to query. Then, pass a ``$search`` or ``$searchMeta`` stage in an aggregation pipeline parameter to the ``raw_aggregate()`` method. -.. important:: +.. tip:: - You cannot use the ``QuerySet`` API to create Atlas Search indexes. - However, you can create an index by exposing your ``MongoClient`` - object directly, on which you can call the PyMongo driver's - ``create_search_index()`` method. To learn how to expose the - ``MongoClient``, see the :ref:`django-client-operations` section - of this guide. - - For instructions on using the PyMongo driver to create an Atlas - Search index, see `Atlas Search and Vector Search Indexes - <{+pymongo-docs+}/indexes/atlas-search-index/>`__ - in the PyMongo documentation. + To learn how to create Atlas Search indexes, see :ref:`django-indexes-atlas-search` + in the Create Indexes guide. This example runs an Atlas Search query by passing the ``$search`` pipeline stage to the ``raw_aggregate()`` method. The code performs the following From 96525632b8b35ff526da870d8516b6b10393bd3a Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 11:03:02 -0400 Subject: [PATCH 05/13] edits --- source/interact-data.txt | 2 +- source/interact-data/transactions.txt | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/source/interact-data.txt b/source/interact-data.txt index 66be919..72f1e21 100644 --- a/source/interact-data.txt +++ b/source/interact-data.txt @@ -38,5 +38,5 @@ MongoDB data. - :ref:`django-raw-queries`: Learn how to use MongoDB's aggregation pipeline syntax or the PyMongo driver to query your data. -- :ref:`django-transactions`: Learn how to use {+framework+}'s transactions API +- :ref:`django-transactions`: Learn how to use {+framework+}'s transaction API to run data operations within a transaction. diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index fe1b449..ae70819 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -86,7 +86,8 @@ Examples ~~~~~~~~ The following example calls the ``create()`` method within a transaction, -inserting a document into the ``sample_mflix.movies`` collection: +inserting a document into the ``sample_mflix.movies`` collection if the +transaction succeeds: .. literalinclude:: /includes/interact-data/transactions.py :start-after: start-transaction-decorator @@ -96,7 +97,7 @@ inserting a document into the ``sample_mflix.movies`` collection: Alternatively, you can use the ``transaction.atomic()`` context manager to create an atomic block. This example runs the same operation as the -previous example but uses a context manager to start a transaction: +preceding example but uses a context manager to start a transaction: .. literalinclude:: /includes/interact-data/transactions.py :start-after: start-transaction-manager From 48f877ebaf967eafbbe16432711cd96b01caf36c Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 11:07:57 -0400 Subject: [PATCH 06/13] add limitation --- source/limitations-upcoming.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index 79dce3c..c6995ec 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -259,7 +259,8 @@ Database and Collection Support a transaction, and any subsequent atomic blocks have no effect. - Migration operations do not run inside a transaction, which differs from {+framework+}'s default migration behavior. - - ✓ + - Your MongoDB deployment must be a replica set or sharded cluster. + - *Partially Supported*. Django Features --------------- From fab0362d06b19a284746a93a7a1cdffb88d44c6a Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 15:28:40 -0400 Subject: [PATCH 07/13] edit --- source/interact-data/transactions.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index ae70819..b5c83ef 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -82,9 +82,6 @@ This decorator guarantees the atomicity of any database operations within the function. If the function successfully completes, the changes are committed MongoDB. -Examples -~~~~~~~~ - The following example calls the ``create()`` method within a transaction, inserting a document into the ``sample_mflix.movies`` collection if the transaction succeeds: From 7b36dedfdaf153b310ee95535fc05653850faef2 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 15:48:43 -0400 Subject: [PATCH 08/13] add example --- source/includes/interact-data/transactions.py | 18 ++++++++++++++++++ source/interact-data/transactions.txt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/source/includes/interact-data/transactions.py b/source/includes/interact-data/transactions.py index 281ba20..1052af2 100644 --- a/source/includes/interact-data/transactions.py +++ b/source/includes/interact-data/transactions.py @@ -21,6 +21,24 @@ def insert_movie_transaction(): ) # end-transaction-manager +# start-callback +def get_horror_comedies(): + movies = Movie.objects.filter(genres=["Horror", "Comedy"]) + for m in movies: + print(f"Title: {m.title}, runtime: {m.runtime}") + +def insert_movie_with_callback(): + with transaction.atomic(): + Movie.objects.create( + title="The Substance", + runtime=140, + genres=["Horror", "Comedy"] + ) + + # Registers the callback to run only after the transaction commits + transaction.on_commit(get_horror_comedies) +# end-callback + # start-handle-errors movie = Movie.objects.get( title="Jurassic Park", diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index b5c83ef..6dc9c1d 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -102,6 +102,24 @@ preceding example but uses a context manager to start a transaction: :language: python :copyable: +Run Callbacks After a Transaction +--------------------------------- + +If you want to run certain operations only if a transaction successfully +completes, you can us the ``on_commit()`` method. This method allows you to +register callbacks that run after a transaction is committed to the +database. Pass a function, or any callable object, as an argument to +``on_commit()``. + +The following example queries for movies that have a genre value of +``["Horror", "Comedy"]`` only after a related database transaction completes: + +.. literalinclude:: /includes/interact-data/transactions.py + :start-after: start-callback + :end-before: end-callback + :language: python + :copyable: + Handle Transaction Errors ------------------------- From 4d0fdb3c85d85eece66983e1adf70cd2df84b87f Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 15:50:31 -0400 Subject: [PATCH 09/13] wording --- source/interact-data/transactions.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 6dc9c1d..902e3c1 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -105,13 +105,13 @@ preceding example but uses a context manager to start a transaction: Run Callbacks After a Transaction --------------------------------- -If you want to run certain operations only if a transaction successfully -completes, you can us the ``on_commit()`` method. This method allows you to +If you want to perform certain actions only if a transaction successfully +completes, you can use the ``on_commit()`` method. This method allows you to register callbacks that run after a transaction is committed to the database. Pass a function, or any callable object, as an argument to ``on_commit()``. -The following example queries for movies that have a genre value of +The following example queries for movies that have a ``genre`` value of ``["Horror", "Comedy"]`` only after a related database transaction completes: .. literalinclude:: /includes/interact-data/transactions.py From 90d24a81012c281258de9b47f70cd3a658952613 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 11 Jul 2025 15:54:17 -0400 Subject: [PATCH 10/13] fix --- source/includes/interact-data/transactions.py | 3 +-- source/interact-data/transactions.txt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/source/includes/interact-data/transactions.py b/source/includes/interact-data/transactions.py index 1052af2..8072b35 100644 --- a/source/includes/interact-data/transactions.py +++ b/source/includes/interact-data/transactions.py @@ -34,8 +34,7 @@ def insert_movie_with_callback(): runtime=140, genres=["Horror", "Comedy"] ) - - # Registers the callback to run only after the transaction commits + transaction.on_commit(get_horror_comedies) # end-callback diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 902e3c1..6093861 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -106,7 +106,7 @@ Run Callbacks After a Transaction --------------------------------- If you want to perform certain actions only if a transaction successfully -completes, you can use the ``on_commit()`` method. This method allows you to +completes, you can use the ``transaction.on_commit()`` method. This method allows you to register callbacks that run after a transaction is committed to the database. Pass a function, or any callable object, as an argument to ``on_commit()``. From 72b93db0b9733082f6b0703915be18cdc8c58851 Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 14 Jul 2025 11:36:41 -0400 Subject: [PATCH 11/13] feedback --- source/includes/interact-data/transactions.py | 17 ++++++++++------- source/interact-data/transactions.txt | 19 ++++++++++--------- source/limitations-upcoming.txt | 3 +-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/source/includes/interact-data/transactions.py b/source/includes/interact-data/transactions.py index 8072b35..916f5a6 100644 --- a/source/includes/interact-data/transactions.py +++ b/source/includes/interact-data/transactions.py @@ -34,18 +34,21 @@ def insert_movie_with_callback(): runtime=140, genres=["Horror", "Comedy"] ) - transaction.on_commit(get_horror_comedies) # end-callback # start-handle-errors -movie = Movie.objects.get( - title="Jurassic Park", - released=timezone.make_aware(datetime(1993, 6, 11)) -) +movie = Movie.objects.get(title="Jurassic Park") +movie.title = "Jurassic Park I" try: with transaction.atomic(): - movie.update(title="Jurassic Park I") + movie.save() except DatabaseError: - movie.update(title="Jurassic Park") + movie.title = "Jurassic Park" + +if movie.title == "Jurassic Park I": + movie.plot = "An industrialist invites experts to visit his theme park of cloned dinosaurs. After a power failure," \ + " the creatures run loose, putting everyone's lives, including his grandchildren's, in danger." + movie.save() + # end-handle-errors \ No newline at end of file diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 6093861..d61bfd0 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -24,7 +24,7 @@ In this guide, you can learn how to use {+django-odm+} to perform **transactions**. Transactions allow you to run a series of operations that change data only if the entire transaction is committed. If any operation in the transaction does not succeed, {+django-odm+} stops the -transaction and discards all data changes before they ever become +transaction and discards all changes to the data before they ever become visible. This feature is called **atomicity**. In MongoDB, transactions run within logical sessions. A @@ -80,10 +80,10 @@ To start a database transaction, define an atomic block of code by adding the ``@transaction.atomic`` decorator above your function. This decorator guarantees the atomicity of any database operations within the function. If the function successfully completes, the -changes are committed MongoDB. +changes are committed to MongoDB. The following example calls the ``create()`` method within a transaction, -inserting a document into the ``sample_mflix.movies`` collection if the +which inserts a document into the ``sample_mflix.movies`` collection if the transaction succeeds: .. literalinclude:: /includes/interact-data/transactions.py @@ -105,8 +105,8 @@ preceding example but uses a context manager to start a transaction: Run Callbacks After a Transaction --------------------------------- -If you want to perform certain actions only if a transaction successfully -completes, you can use the ``transaction.on_commit()`` method. This method allows you to +To perform certain actions only if a transaction successfully completes, +you can use the ``transaction.on_commit()`` function. This function allows you to register callbacks that run after a transaction is committed to the database. Pass a function, or any callable object, as an argument to ``on_commit()``. @@ -124,11 +124,12 @@ Handle Transaction Errors ------------------------- To handle exceptions that occur during a transaction, add error handling -logic around your atomic code block. If you include error handing logic inside -the atomic block, {+framework+} might ignore these errors, resulting in unexpected -behavior. +logic around your atomic code block. If you handle errors inside +the atomic block, you might obscure these errors from {+framework+}. Since +{+framework+} uses errors to determine whether to commit or roll +back a transaction, this can cause unexpected behavior. -If a transaction does not succeed, your application does not revert any changes made +If a transaction does not succeed, Django does not revert any changes made to a model's fields. To avoid inconsistencies between your models and database documents, you might need to manually restore the original field values. diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index c6995ec..1b3c2b8 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -257,8 +257,7 @@ Database and Collection Support - If a transaction generates an error, the transaction is no longer usable. - Savepoints, or nested atomic blocks, are not supported. The outermost atomic block starts a transaction, and any subsequent atomic blocks have no effect. - - Migration operations do not run inside a transaction, which differs from {+framework+}'s - default migration behavior. + - Migration operations do not run inside a transaction. - Your MongoDB deployment must be a replica set or sharded cluster. - *Partially Supported*. From d1c6872804d0f00392dcea805b2c3d85cb81a236 Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 14 Jul 2025 13:17:59 -0400 Subject: [PATCH 12/13] edit --- source/interact-data/transactions.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index d61bfd0..419e1a3 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -145,6 +145,10 @@ The following example includes error handling logic that reverts the modified :language: python :copyable: +Since the code performs a second database operation based on the +model's ``title`` value, reverting the change if the transaction errors +prevents further data inconsistencies. + Additional Information ---------------------- From 0d93cbb35bb681ae254159a2b0d1cd5da9618243 Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 14 Jul 2025 15:30:24 -0400 Subject: [PATCH 13/13] feedback 2 --- source/interact-data/transactions.txt | 4 ++-- source/limitations-upcoming.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/interact-data/transactions.txt b/source/interact-data/transactions.txt index 419e1a3..0123a96 100644 --- a/source/interact-data/transactions.txt +++ b/source/interact-data/transactions.txt @@ -44,7 +44,7 @@ manually start a session before running a transaction. {+django-odm+}'s support for the {+framework+} transaction API has several limitations. To view a list of limitations, see :ref:`Database and Collection Support ` - in the Django and MongoDB Feature Compatibility guide. + in the {+framework+} and MongoDB Feature Compatibility guide. Sample Data ~~~~~~~~~~~ @@ -129,7 +129,7 @@ the atomic block, you might obscure these errors from {+framework+}. Since {+framework+} uses errors to determine whether to commit or roll back a transaction, this can cause unexpected behavior. -If a transaction does not succeed, Django does not revert any changes made +If a transaction does not succeed, {+framework+} does not revert any changes made to a model's fields. To avoid inconsistencies between your models and database documents, you might need to manually restore the original field values. diff --git a/source/limitations-upcoming.txt b/source/limitations-upcoming.txt index 1b3c2b8..d09ab74 100644 --- a/source/limitations-upcoming.txt +++ b/source/limitations-upcoming.txt @@ -250,7 +250,7 @@ Database and Collection Support - ✓ * - Transactions - - *Partially Supported*. You can use {+framework+}'s transactions API with the + - ✓ You can use {+framework+}'s transactions API with the following limitations: - ``QuerySet.union()`` is not supported within a transaction. @@ -259,7 +259,7 @@ Database and Collection Support a transaction, and any subsequent atomic blocks have no effect. - Migration operations do not run inside a transaction. - Your MongoDB deployment must be a replica set or sharded cluster. - - *Partially Supported*. + - ✓ Django Features ---------------