summaryrefslogtreecommitdiff
path: root/django
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 /django
parent6103059592c6c1bd171977d26b76ee475175043c (diff)
Fixed #34112 -- Added async-compatible interface to Model methods.
Thanks Adam Johnson for the review.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/base.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 584db319da..2eb7ba7e9b 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -4,6 +4,8 @@ import warnings
from functools import partialmethod
from itertools import chain
+from asgiref.sync import sync_to_async
+
import django
from django.apps import apps
from django.conf import settings
@@ -737,6 +739,9 @@ class Model(metaclass=ModelBase):
self._state.db = db_instance._state.db
+ async def arefresh_from_db(self, using=None, fields=None):
+ return await sync_to_async(self.refresh_from_db)(using=using, fields=fields)
+
def serializable_value(self, field_name):
"""
Return the value of the field name for this instance. If the field is
@@ -810,6 +815,18 @@ class Model(metaclass=ModelBase):
save.alters_data = True
+ async def asave(
+ self, force_insert=False, force_update=False, using=None, update_fields=None
+ ):
+ return await sync_to_async(self.save)(
+ force_insert=force_insert,
+ force_update=force_update,
+ using=using,
+ update_fields=update_fields,
+ )
+
+ asave.alters_data = True
+
def save_base(
self,
raw=False,
@@ -1111,6 +1128,14 @@ class Model(metaclass=ModelBase):
delete.alters_data = True
+ async def adelete(self, using=None, keep_parents=False):
+ return await sync_to_async(self.delete)(
+ using=using,
+ keep_parents=keep_parents,
+ )
+
+ adelete.alters_data = True
+
def _get_FIELD_display(self, field):
value = getattr(self, field.attname)
choices_dict = dict(make_hashable(field.flatchoices))