1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #34112 -- Added async-compatible interface to Model methods.

Thanks Adam Johnson for the review.
This commit is contained in:
DevilsAutumn
2022-10-30 22:21:54 +05:30
committed by Mariusz Felisiak
parent 6103059592
commit d5bcdf858d
6 changed files with 86 additions and 0 deletions

View File

@@ -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))