#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
               Reporter:  Mariusz    |          Owner:  nobody
  Felisiak                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.1
  layer (models, ORM)                |
               Severity:  Release    |       Keywords:  async
  blocker                            |
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Async-compatible interface was added to `QuerySet` in
 58b27e0dbb3d31ca1438790870b2b51ecdb10500. Unfortunately, it also added
 (unintentionally) async `acreate()`, `aget_or_create()`, and
 `aupdate_or_create()` methods to related managers. Moreover they don't
 call `create()`, `get_or_create()`, and `update_or_create()` respectively
 from a related manager but from the `QuerySet`.

 We should add a proper versions to related managers, e.g.
 {{{#!diff
 diff --git a/django/db/models/fields/related_descriptors.py
 b/django/db/models/fields/related_descriptors.py
 index 04c956bd1e..1cba654f06 100644
 --- a/django/db/models/fields/related_descriptors.py
 +++ b/django/db/models/fields/related_descriptors.py
 @@ -62,6 +62,7 @@ and two directions (forward and reverse) for a total of
 six combinations.
     If you're looking for ``ForwardManyToManyDescriptor`` or
     ``ReverseManyToManyDescriptor``, use ``ManyToManyDescriptor`` instead.
  """
 +from asgiref.sync import sync_to_async

  from django.core.exceptions import FieldError
  from django.db import (
 @@ -793,6 +794,11 @@ def create_reverse_many_to_one_manager(superclass,
 rel):

          create.alters_data = True

 +        async def acreate(self, **kwargs):
 +            return await sync_to_async(self.create)(**kwargs)
 +
 +        acreate.alters_data = True
 +
          def get_or_create(self, **kwargs):
              self._check_fk_val()
              kwargs[self.field.name] = self.instance
 @@ -1191,6 +1197,11 @@ def create_forward_many_to_many_manager(superclass,
 rel, reverse):

          create.alters_data = True

 +        async def acreate(self, **kwargs):
 +            return await sync_to_async(self.create)(**kwargs)
 +
 +        acreate.alters_data = True
 +
          def get_or_create(self, *, through_defaults=None, **kwargs):
              db = router.db_for_write(self.instance.__class__,
 instance=self.instance)
              obj, created = super(ManyRelatedManager,
 self.db_manager(db)).get_or_create(

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34139>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018441af5b7c-30ccd156-cec8-4111-a867-003f0547fd83-000000%40eu-central-1.amazonses.com.

Reply via email to