Opened 2 years ago
Closed 2 years ago
#34112 closed New feature (fixed)
Add async interface to Model
Reported by: | Adam Johnson | Owned by: | Bhuvnesh |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Following #33646, it would be convenient to also have an async interface on Model
methods that use the database:
Model.asave()
to mirrorsave()
Model.adelete()
to mirrordelete()
Model.arefresh_from_db()
to mirrorrefresh_from_db()
Allowing usage like:
book = await Book.objects.aget(title=title) book.pages = ... await book.asave()
There’s already a way to effectively call save for new objects only, through the QuerySet.acreate()
method, exposed on managers.
Model
also has some database-touching methods used in form validation, like validate_constraints()
. But if we aren’t intending on exposing the forms API as async, yet, then these could be deferred
Change History (14)
comment:1 by , 2 years ago
Summary: | Add async-compatible interface to Model → Add async interface to Model |
---|
comment:2 by , 2 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 2 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 2 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:6 by , 2 years ago
Hi, i would like to work on this issue, just wanted to know if the approach will be same as in #33646 i.e. using sync_to_async
?
Also a regression test for atleast asave() would be really helpful.
comment:7 by , 2 years ago
Hi, i would like to work on this issue, just wanted to know if the approach will be same as in #33646 i.e. using sync_to_async?
Yes.
Also a regression test for atleast asave() would be really helpful.
Please write the appropriate tests in your PR.
comment:9 by , 2 years ago
Has patch: | set |
---|
comment:10 by , 2 years ago
Patch needs improvement: | set |
---|
comment:12 by , 2 years ago
Patch needs improvement: | set |
---|
comment:13 by , 2 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Sounds reasonable, thanks.