Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and return model #637

Open
lsabi opened this issue Feb 6, 2021 · 1 comment
Open

Update and return model #637

lsabi opened this issue Feb 6, 2021 · 1 comment

Comments

@lsabi
Copy link

lsabi commented Feb 6, 2021

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

@andcan86
Copy link

andcan86 commented Dec 2, 2021

I have the same problem/question. So far I am updating the records with, say Model, and right afterwards I do the selection:

await Model.filter(..).update(*new kwargs)
await Model.filter(*new kwargs)

which is awful but works. Aiming for better solutions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants