Merge pull request #557 from MojixCoder/master

Update get_or_create method
This commit is contained in:
collerek
2022-03-28 13:04:55 +02:00
committed by GitHub
18 changed files with 150 additions and 62 deletions

View File

@ -9,6 +9,7 @@ from typing import ( # noqa: I100, I201
Sequence,
Set,
TYPE_CHECKING,
Tuple,
Type,
TypeVar,
Union,
@ -488,23 +489,31 @@ class QuerysetProxy(Generic[T]):
)
return len(children)
async def get_or_create(self, *args: Any, **kwargs: Any) -> "T":
async def get_or_create(
self,
_defaults: Optional[Dict[str, Any]] = None,
*args: Any,
**kwargs: Any,
) -> Tuple["T", bool]:
"""
Combination of create and get methods.
Tries to get a row meeting the criteria fro kwargs
and if `NoMatch` exception is raised
it creates a new one with given kwargs.
it creates a new one with given kwargs and _defaults.
:param kwargs: fields names and proper value types
:type kwargs: Any
:return: returned or created Model
:rtype: Model
:param _defaults: default values for creating object
:type _defaults: Optional[Dict[str, Any]]
:return: model instance and a boolean
:rtype: Tuple("T", bool)
"""
try:
return await self.get(*args, **kwargs)
except ormar.NoMatch:
return await self.create(**kwargs)
return await self.get(*args, **kwargs), False
except NoMatch:
_defaults = _defaults or {}
return await self.create(**{**kwargs, **_defaults}), True
async def update_or_create(self, **kwargs: Any) -> "T":
"""