tortoise / tortoise/tortoise-orm

Update and return model

Open
#637 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
I'm not sure if there's already a way to deal with it, but I couldn't find anything, so in case the functionality is implemented, it is not in the docs (or it's difficult to find it).

Describe the solution you'd like
In my case I'm using fastapi to update a model, call it myModel, which has M2M relations. In particular, I need to update the values of the fields in myModel and the M2M relations. I would like to update the fields and get the newly updated object, on which I can remove/add the M2M relations.

Describe alternatives you've considered
await myModel.get(id=id).update(
**input.dict(exclude_unset=True)
)
model = await myModel.get(id=id).prefetch_related("other_relation", "another_relation")
[model.other_relation.remove(x) for x in model.other_relations]
for id in other_rel_ids:
await model.other_relation.add(Other_relation.get(id=id))

# Similarly, for "another_relation"

In this case I need to execute the update query, then get the model and only then start updating the M2M relations. When tables are small it does not influence much performances, but for bigger tables it becomes a problem. Is there a way to update and get the model with the updated fields at once?

Additional context
Python: 3.8.5
Tortoise-orm: 0.16.20

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by reviewing the existing Tortoise ORM update API and related documentation, then trace how model updates and many-to-many relations are handled. Done means the supported workflow can update fields, return the updated model, and allow subsequent many-to-many changes without the extra fetch described.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.