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 mirror save()
  • Model.adelete() to mirror delete()
  • Model.arefresh_from_db() to mirror refresh_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 Adam Johnson, 2 years ago

Summary: Add async-compatible interface to ModelAdd async interface to Model

comment:2 by Leo Tom, 2 years ago

Owner: changed from nobody to Leo Tom
Status: newassigned

comment:3 by Mariusz Felisiak, 2 years ago

Triage Stage: UnreviewedAccepted

Sounds reasonable, thanks.

comment:4 by Leo Tom, 2 years ago

Owner: Leo Tom removed
Status: assignednew

comment:5 by Bhuvnesh, 2 years ago

Owner: set to Bhuvnesh
Status: newassigned

comment:6 by Bhuvnesh, 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.

Last edited 2 years ago by Bhuvnesh (previous) (diff)

comment:7 by Adam Johnson, 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:8 by Bhuvnesh, 2 years ago

comment:9 by Bhuvnesh, 2 years ago

Has patch: set

comment:10 by Adam Johnson, 2 years ago

Patch needs improvement: set

comment:11 by Bhuvnesh, 2 years ago

Patch needs improvement: unset

comment:12 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:13 by Mariusz Felisiak, 2 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:14 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In d5bcdf85:

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

Thanks Adam Johnson for the review.

Note: See TracTickets for help on using tickets.
Back to Top