summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDevilsAutumn <bhuvnesh875@gmail.com>2022-10-30 22:21:54 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-11-02 09:14:17 +0100
commitd5bcdf858d962d02de925603c17986980f03729a (patch)
tree367182dbc54c46faa2196b7f047eceeda62ec650 /docs
parent6103059592c6c1bd171977d26b76ee475175043c (diff)
Fixed #34112 -- Added async-compatible interface to Model methods.
Thanks Adam Johnson for the review.
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/models/instances.txt21
-rw-r--r--docs/releases/4.2.txt4
-rw-r--r--docs/topics/async.txt10
3 files changed, 35 insertions, 0 deletions
diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt
index f0306a7460..f9f11cac0d 100644
--- a/docs/ref/models/instances.txt
+++ b/docs/ref/models/instances.txt
@@ -132,6 +132,9 @@ value from the database::
>>> obj.field # Loads the field from the database
.. method:: Model.refresh_from_db(using=None, fields=None)
+.. method:: Model.arefresh_from_db(using=None, fields=None)
+
+*Asynchronous version*: ``arefresh_from_db()``
If you need to reload a model's values from the database, you can use the
``refresh_from_db()`` method. When this method is called without arguments the
@@ -188,6 +191,10 @@ all of the instance's fields when a deferred field is reloaded::
A helper method that returns a set containing the attribute names of all those
fields that are currently deferred for this model.
+.. versionchanged:: 4.2
+
+ ``arefresh_from_db()`` method was added.
+
.. _validating-objects:
Validating objects
@@ -406,6 +413,9 @@ Saving objects
To save an object back to the database, call ``save()``:
.. method:: Model.save(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None)
+.. method:: Model.asave(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None)
+
+*Asynchronous version*: ``asave()``
For details on using the ``force_insert`` and ``force_update`` arguments, see
:ref:`ref-models-force-insert`. Details about the ``update_fields`` argument
@@ -416,6 +426,10 @@ method. See :ref:`overriding-model-methods` for more details.
The model save process also has some subtleties; see the sections below.
+.. versionchanged:: 4.2
+
+ ``asave()`` method was added.
+
Auto-incrementing primary keys
------------------------------
@@ -644,6 +658,9 @@ Deleting objects
================
.. method:: Model.delete(using=DEFAULT_DB_ALIAS, keep_parents=False)
+.. method:: Model.adelete(using=DEFAULT_DB_ALIAS, keep_parents=False)
+
+*Asynchronous version*: ``adelete()``
Issues an SQL ``DELETE`` for the object. This only deletes the object in the
database; the Python instance will still exist and will still have data in
@@ -660,6 +677,10 @@ Sometimes with :ref:`multi-table inheritance <multi-table-inheritance>` you may
want to delete only a child model's data. Specifying ``keep_parents=True`` will
keep the parent model's data.
+.. versionchanged:: 4.2
+
+ ``adelete()`` method was added.
+
Pickling objects
================
diff --git a/docs/releases/4.2.txt b/docs/releases/4.2.txt
index 4f7bda7080..ba01bf12e5 100644
--- a/docs/releases/4.2.txt
+++ b/docs/releases/4.2.txt
@@ -239,6 +239,10 @@ Models
* :class:`F() <django.db.models.F>` expressions that output ``BooleanField``
can now be negated using ``~F()`` (inversion operator).
+* ``Model`` now provides asynchronous versions of some methods that use the
+ database, using an ``a`` prefix: :meth:`~.Model.adelete`,
+ :meth:`~.Model.arefresh_from_db`, and :meth:`~.Model.asave`.
+
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
diff --git a/docs/topics/async.txt b/docs/topics/async.txt
index 9e2785c351..2b9b1a85d9 100644
--- a/docs/topics/async.txt
+++ b/docs/topics/async.txt
@@ -91,10 +91,20 @@ Detailed notes can be found in :ref:`async-queries`, but in short:
* ``async for`` is supported on all QuerySets (including the output of
``values()`` and ``values_list()``.)
+Django also supports some asynchronous model methods that use the database::
+
+ async def make_book(...):
+ book = Book(...)
+ await book.asave(using="secondary")
+
Transactions do not yet work in async mode. If you have a piece of code that
needs transactions behavior, we recommend you write that piece as a single
synchronous function and call it using :func:`sync_to_async`.
+.. versionchanged:: 4.2
+
+ Asynchronous model interface was added.
+
Performance
-----------