[Django] #34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as intended on related managers.

5 views
Skip to first unread message

Django

unread,
Nov 4, 2022, 4:08:45 AM11/4/22
to django-...@googlegroups.com
#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.

Django

unread,
Nov 4, 2022, 4:38:04 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Janzen):

* cc: Jon Janzen (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:1>

Django

unread,
Nov 4, 2022, 5:00:36 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:2>

Django

unread,
Nov 4, 2022, 5:20:52 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Jon, would you like to prepare a patch? (against the `main` branch.)

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:3>

Django

unread,
Nov 4, 2022, 6:01:08 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned

Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Janzen):

* owner: nobody => Jon Janzen
* status: new => assigned


Comment:

Yeah I can take care of that. I’ll be traveling for the next 2 days then
recovering from jetlag and then back on the road again, so I just want to
set expectations that this won’t be immediate :(

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:4>

Django

unread,
Nov 4, 2022, 6:03:45 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:4 Jon Janzen]:


> Yeah I can take care of that. I’ll be traveling for the next 2 days then
recovering from jetlag and then back on the road again, so I just want to
set expectations that this won’t be immediate :(

Thanks and don't worry, it's a release blocker so we have time to the end
of November.

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:5>

Django

unread,
Nov 4, 2022, 10:27:09 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Janzen):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/16256 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:6>

Django

unread,
Nov 4, 2022, 10:27:35 AM11/4/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jon Janzen):

I ended up having a longer layover than intended, so I was able to write
this up.

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:7>

Django

unread,
Nov 7, 2022, 2:13:51 AM11/7/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:8>

Django

unread,
Nov 7, 2022, 8:55:33 AM11/7/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Janzen):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:9>

Django

unread,
Nov 8, 2022, 12:23:00 AM11/8/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: async | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:10>

Django

unread,
Nov 8, 2022, 2:13:33 AM11/8/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: closed

Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: async | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7b94847e384b1a8c05a7d4c8778958c0290bdf9a" 7b94847]:
{{{
#!CommitTicketReference repository=""
revision="7b94847e384b1a8c05a7d4c8778958c0290bdf9a"
Fixed #34139 -- Fixed acreate(), aget_or_create(), and aupdate_or_create()
methods for related managers.

Bug in 58b27e0dbb3d31ca1438790870b2b51ecdb10500.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:11>

Django

unread,
Nov 8, 2022, 2:14:48 AM11/8/22
to django-...@googlegroups.com
#34139: acreate(), aget_or_create(), and aupdate_or_create() doesn't work as
intended on related managers.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: Jon
| Janzen
Type: Bug | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: async | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9fb57fcc703749269987f54f9ee1d71bd9a2dbca" 9fb57fc]:
{{{
#!CommitTicketReference repository=""
revision="9fb57fcc703749269987f54f9ee1d71bd9a2dbca"
[4.1.x] Fixed #34139 -- Fixed acreate(), aget_or_create(), and


aupdate_or_create() methods for related managers.

Bug in 58b27e0dbb3d31ca1438790870b2b51ecdb10500.

Backport of 7b94847e384b1a8c05a7d4c8778958c0290bdf9a from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34139#comment:12>

Reply all
Reply to author
Forward
0 new messages