MCPcopy
hub / github.com/tortoise/tortoise-orm / clone

Method clone

tortoise/models.py:918–939  ·  view source on GitHub ↗

Create a new clone of the object that when you do a ``.save()`` will create a new record. :param pk: An optionally required value if the model doesn't generate its own primary key. Any value you specify here will always be used. :return: A copy of the current ob

(self: MODEL, pk: Any = EMPTY)

Source from the content-addressed store, hash-verified

916 raise ObjectDoesNotExistError(cls, cls._meta.pk_attr, key)
917
918 def clone(self: MODEL, pk: Any = EMPTY) -> MODEL:
919 """
920 Create a new clone of the object that when you do a ``.save()`` will create a new record.
921
922 :param pk: An optionally required value if the model doesn't generate its own primary key.
923 Any value you specify here will always be used.
924 :return: A copy of the current object without primary key information.
925 :raises ParamsError: If pk is required but not provided.
926 """
927 obj = copy(self)
928 if pk is EMPTY:
929 pk_field: Field = self._meta.pk
930 if pk_field.generated is False and pk_field.default is None:
931 raise ParamsError(
932 f"{self._meta.full_name} requires explicit primary key. Please use .clone(pk=<value>)"
933 )
934 else:
935 obj.pk = None
936 else:
937 obj.pk = pk
938 obj._saved_in_db = False
939 return obj
940
941 @classmethod
942 def construct(cls: type[MODEL], _saved_in_db: bool = False, **kwargs: Any) -> MODEL:

Callers 9

test_clone_pk_requiredFunction · 0.45
test_cloneFunction · 0.45
test_clone_with_pkFunction · 0.45
test_clone_from_dbFunction · 0.45
test_noid_cloneFunction · 0.45
test_noid_clone_with_pkFunction · 0.45
test_noid_clone_from_dbFunction · 0.45

Calls 1

ParamsErrorClass · 0.90

Tested by 9

test_clone_pk_requiredFunction · 0.36
test_cloneFunction · 0.36
test_clone_with_pkFunction · 0.36
test_clone_from_dbFunction · 0.36
test_noid_cloneFunction · 0.36
test_noid_clone_with_pkFunction · 0.36
test_noid_clone_from_dbFunction · 0.36