MCPcopy Index your code
hub / github.com/tortoise/tortoise-orm / get_or_create

Method get_or_create

tortoise/models.py:1269–1292  ·  view source on GitHub ↗

Fetches the object if exists (filtering on the provided parameters), else creates an instance with any unspecified parameters as default values. :param defaults: Default values to be added to a created instance if it can't be fetched. :param using_db: Specific DB co

(
        cls,
        defaults: dict | None = None,
        using_db: BaseDBAsyncClient | None = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

1267
1268 @classmethod
1269 async def get_or_create(
1270 cls,
1271 defaults: dict | None = None,
1272 using_db: BaseDBAsyncClient | None = None,
1273 **kwargs: Any,
1274 ) -> tuple[Self, bool]:
1275 """
1276 Fetches the object if exists (filtering on the provided parameters),
1277 else creates an instance with any unspecified parameters as default values.
1278
1279 :param defaults: Default values to be added to a created instance if it can't be fetched.
1280 :param using_db: Specific DB connection to use instead of default bound
1281 :param kwargs: Query parameters.
1282 :raises IntegrityError: If create failed
1283 :raises TransactionManagementError: If transaction error
1284 :raises ParamsError: If defaults conflict with kwargs
1285 """
1286 if not defaults:
1287 defaults = {}
1288 db = using_db or cls._choose_db(True)
1289 try:
1290 return await cls.filter(**kwargs).using_db(db).get(), False
1291 except DoesNotExist:
1292 return await cls._create_or_get(db, defaults, **kwargs)
1293
1294 @classmethod
1295 async def _create_or_get(

Calls 5

using_dbMethod · 0.80
_create_or_getMethod · 0.80
_choose_dbMethod · 0.45
getMethod · 0.45
filterMethod · 0.45