-
Notifications
You must be signed in to change notification settings - Fork 7
DOCSP-51036: Transactions #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
073c271
07a82ac
f1560c9
005217f
9652563
48f877e
fab0362
7b36ded
4d0fdb3
90d24a8
72b93db
d1c6872
0d93cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.db import transaction, DatabaseError | ||
from sample_mflix.models import Movie | ||
|
||
# start-transaction-decorator | ||
@transaction.atomic | ||
def insert_movie_transaction(): | ||
Movie.objects.create( | ||
title="Poor Things", | ||
runtime=141, | ||
genres=["Comedy", "Romance"] | ||
) | ||
# end-transaction-decorator | ||
|
||
# start-transaction-manager | ||
def insert_movie_transaction(): | ||
with transaction.atomic(): | ||
Movie.objects.create( | ||
title="Poor Things", | ||
runtime=141, | ||
genres=["Comedy", "Romance"] | ||
) | ||
# 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"] | ||
) | ||
|
||
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)) | ||
|
||
) | ||
try: | ||
with transaction.atomic(): | ||
movie.update(title="Jurassic Park I") | ||
|
||
except DatabaseError: | ||
movie.update(title="Jurassic Park") | ||
|
||
# end-handle-errors |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,151 @@ | ||||||
.. _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 {+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 data changes before they ever become | |
transaction and discards all changes to the data before they ever become |
Consider this for clarity
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the Django and MongoDB Feature Compatibility guide. | |
in the {+framework+} and MongoDB Feature Compatibility guide. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inserting a document into the ``sample_mflix.movies`` collection if the | |
which inserts a document into the ``sample_mflix.movies`` collection if the |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to perform certain actions only if a transaction successfully | |
To perform certain actions only if a transaction successfully |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
completes, you can use the ``transaction.on_commit()`` method. This method allows you to | |
completes, use the ``transaction.on_commit()`` method. This method allows you to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on_commit()
is a function, not a method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
database. Pass a function, or any callable object, as an argument to | |
database. To use this method, pass a function, or any callable object, as an argument to |
I think this is what this says
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll keep as is to avoid using too many commas, since I think the "to use..." is implied
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the meaning of "{+framework+} might ignore these errors," is probably unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some more detail here
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"your application" -> Django
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,8 @@ Query Support | |
the :ref:`raw_aggregate() method <django-raw-queries-search>`. | ||
- ✓ | ||
|
||
.. _django-feature-compat-db-coll: | ||
|
||
Database and Collection Support | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
@@ -248,8 +250,17 @@ 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. | ||
- 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. | ||
|
||
- Your MongoDB deployment must be a replica set or sharded cluster. | ||
- *Partially Supported*. | ||
|
||
Django Features | ||
--------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chop the blank line?