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

add an option to return fields on update #1357

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ class UpdateQuery(AwaitableQuery):
"orderings",
"limit",
"values",
"returning_columns",
)

def __init__(
Expand All @@ -1054,6 +1055,7 @@ def __init__(
self.limit = limit
self.orderings = orderings
self.values: List[Any] = []
self.returning_columns = []

def _make_query(self) -> None:
table = self.model._meta.basetable
Expand Down Expand Up @@ -1100,15 +1102,22 @@ def _make_query(self) -> None:
self.query = self.query.set(db_field, executor.parameter(count))
self.values.append(value)
count += 1
if self.returning_columns:
self.query = self.query.returning(*self.return_columns)

def __await__(self) -> Generator[Any, None, int]:
if self._db is None:
self._db = self._choose_db(True) # type: ignore
self._make_query()
return self._execute().__await__()

async def _execute(self) -> int:
return (await self._db.execute_query(str(self.query), self.values))[0]
async def _execute(self) -> List[dict]:
res = await self._db.execute_query_dict(str(self.query))
return res

def returning(self, *columns):
self.return_columns = columns
return self


class DeleteQuery(AwaitableQuery):
Expand Down