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

Method update_or_create

tortoise/models.py:1340–1361  ·  view source on GitHub ↗

A convenience method for updating an object with the given kwargs, creating a new one if necessary. :param defaults: Default values used to update the object. :param using_db: Specific DB connection to use instead of default bound :param kwargs: Query parameters.

(
        cls: type[MODEL],
        defaults: dict | None = None,
        using_db: BaseDBAsyncClient | None = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

1338
1339 @classmethod
1340 async def update_or_create(
1341 cls: type[MODEL],
1342 defaults: dict | None = None,
1343 using_db: BaseDBAsyncClient | None = None,
1344 **kwargs: Any,
1345 ) -> tuple[MODEL, bool]:
1346 """
1347 A convenience method for updating an object with the given kwargs, creating a new one if necessary.
1348
1349 :param defaults: Default values used to update the object.
1350 :param using_db: Specific DB connection to use instead of default bound
1351 :param kwargs: Query parameters.
1352 """
1353 if not defaults:
1354 defaults = {}
1355 db = using_db or cls._choose_db(True)
1356 async with in_transaction(connection_name=db.connection_name) as connection:
1357 instance = await cls.select_for_update().using_db(connection).get_or_none(**kwargs)
1358 if instance:
1359 await instance.update_from_dict(defaults).save(using_db=connection)
1360 return instance, False
1361 return await cls._create_or_get(db, defaults, **kwargs)
1362
1363 @classmethod
1364 async def create(

Callers 3

test_update_or_createFunction · 0.80

Calls 8

in_transactionFunction · 0.90
using_dbMethod · 0.80
saveMethod · 0.80
update_from_dictMethod · 0.80
_create_or_getMethod · 0.80
_choose_dbMethod · 0.45
get_or_noneMethod · 0.45
select_for_updateMethod · 0.45

Tested by 3

test_update_or_createFunction · 0.64