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)
| 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: |