Returns a copy of the model. !!! warning "Deprecated" This method is now deprecated; use `model_copy` instead. If you need `include` or `exclude`, use: ```python {test="skip" lint="skip"} data = self.model_dump(include=include, exclude=exclude, round_tr
(
self,
*,
include: AbstractSetIntStr | MappingIntStrAny | None = None,
exclude: AbstractSetIntStr | MappingIntStrAny | None = None,
update: Dict[str, Any] | None = None, # noqa UP006
deep: bool = False,
)
| 1509 | category=None, |
| 1510 | ) |
| 1511 | def copy( |
| 1512 | self, |
| 1513 | *, |
| 1514 | include: AbstractSetIntStr | MappingIntStrAny | None = None, |
| 1515 | exclude: AbstractSetIntStr | MappingIntStrAny | None = None, |
| 1516 | update: Dict[str, Any] | None = None, # noqa UP006 |
| 1517 | deep: bool = False, |
| 1518 | ) -> Self: # pragma: no cover |
| 1519 | """Returns a copy of the model. |
| 1520 | |
| 1521 | !!! warning "Deprecated" |
| 1522 | This method is now deprecated; use `model_copy` instead. |
| 1523 | |
| 1524 | If you need `include` or `exclude`, use: |
| 1525 | |
| 1526 | ```python {test="skip" lint="skip"} |
| 1527 | data = self.model_dump(include=include, exclude=exclude, round_trip=True) |
| 1528 | data = {**data, **(update or {})} |
| 1529 | copied = self.model_validate(data) |
| 1530 | ``` |
| 1531 | |
| 1532 | Args: |
| 1533 | include: Optional set or mapping specifying which fields to include in the copied model. |
| 1534 | exclude: Optional set or mapping specifying which fields to exclude in the copied model. |
| 1535 | update: Optional dictionary of field-value pairs to override field values in the copied model. |
| 1536 | deep: If True, the values of fields that are Pydantic models will be deep-copied. |
| 1537 | |
| 1538 | Returns: |
| 1539 | A copy of the model with included, excluded and updated fields as specified. |
| 1540 | """ |
| 1541 | warnings.warn( |
| 1542 | 'The `copy` method is deprecated; use `model_copy` instead. ' |
| 1543 | 'See the docstring of `BaseModel.copy` for details about how to handle `include` and `exclude`.', |
| 1544 | category=PydanticDeprecatedSince20, |
| 1545 | stacklevel=2, |
| 1546 | ) |
| 1547 | from .deprecated import copy_internals |
| 1548 | |
| 1549 | values = dict( |
| 1550 | copy_internals._iter( |
| 1551 | self, to_dict=False, by_alias=False, include=include, exclude=exclude, exclude_unset=False |
| 1552 | ), |
| 1553 | **(update or {}), |
| 1554 | ) |
| 1555 | if self.__pydantic_private__ is None: |
| 1556 | private = None |
| 1557 | else: |
| 1558 | private = {k: v for k, v in self.__pydantic_private__.items() if v is not PydanticUndefined} |
| 1559 | |
| 1560 | if self.__pydantic_extra__ is None: |
| 1561 | extra: dict[str, Any] | None = None |
| 1562 | else: |
| 1563 | extra = self.__pydantic_extra__.copy() |
| 1564 | for k in list(self.__pydantic_extra__): |
| 1565 | if k not in values: # k was in the exclude |
| 1566 | extra.pop(k) |
| 1567 | for k in list(values): |
| 1568 | if k in self.__pydantic_extra__: # k must have come from extra |