{"id":9664,"library":"django-clone","title":"Django Clone","description":"django-clone is a Django library that facilitates the creation of a deep clone of a Django model instance, including its related objects (Foreign Keys, One-to-One, Many-to-Many). It handles unique fields and provides flexibility for custom attribute overrides during cloning. The current version is 5.5.0, and it maintains an active development pace with regular updates.","status":"active","version":"5.5.0","language":"python","source_language":"en","source_url":"https://github.com/tj-django/django-clone.git","tags":["django","model","clone","copy","utility"],"install":[{"cmd":"pip install django-clone","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core framework dependency for any Django library.","package":"Django"},{"reason":"Required only if cloning MPTT models to preserve tree structure during cloning.","package":"django-mptt","optional":true}],"imports":[{"wrong":"from django_clone import clone_instance","symbol":"CloneMixin","correct":"from model_clone import CloneMixin"},{"wrong":"from django_clone import clone_instance","symbol":"create_copy_of_instance","correct":"from model_clone import create_copy_of_instance"},{"wrong":"from django_clone import clone_instance","symbol":"CloneModelAdmin","correct":"from model_clone import CloneModelAdmin"}],"quickstart":{"code":"import os\nfrom django.db import models\nfrom django_clone import clone_instance\n\n# Minimal Django setup for runnable example\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')\nimport django\ndjango.setup()\n\nclass Author(models.Model):\n    name = models.CharField(max_length=100)\n\n    def __str__(self):\n        return self.name\n\nclass Book(models.Model):\n    title = models.CharField(max_length=100)\n    isbn = models.CharField(max_length=13, unique=True, null=True, blank=True)\n    description = models.TextField(blank=True)\n    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')\n\n    def __str__(self):\n        return self.title\n\n# Create some initial instances\nauthor1 = Author.objects.create(name=\"Jane Doe\")\noriginal_book = Book.objects.create(\n    title=\"The Original Story\", \n    isbn=\"1234567890123\", \n    description=\"A timeless classic.\", \n    author=author1\n)\n\n# Clone the book instance, providing a new unique ISBN\ncloned_book = clone_instance(original_book, attrs={'isbn': '9876543210987', 'title': 'The Cloned Story'})\n\nprint(f\"Original Book: {original_book.title} (ISBN: {original_book.isbn}, Author: {original_book.author.name})\")\nprint(f\"Cloned Book: {cloned_book.title} (ISBN: {cloned_book.isbn}, Author: {cloned_book.author.name})\")\n","lang":"python","description":"This example demonstrates how to clone a Django model instance using `clone_instance`. It creates a `Book` instance with a `ForeignKey` to `Author`, then clones the book, overriding the `isbn` (which is unique) and `title` to avoid conflicts. Note: A minimal Django setup is included for standalone runnability; in a real Django project, you'd omit `os.environ.setdefault` and `django.setup()`."},"warnings":[{"fix":"Update your imports from `from django_clone.utils import clone_model_instance` to `from django_clone import clone_instance`.","message":"The main cloning function `clone_model_instance` was renamed to `clone_instance` and moved from `django_clone.utils` to the top-level `django_clone` package in version 5.0.0.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Remove the `shallow` parameter from your `clone_instance` calls. If you need to prevent certain fields or relations from being cloned, use the `attrs` dictionary to set specific values or handle relationships manually after the clone operation, or use the provided signals for more advanced customization.","message":"The `shallow` parameter of the cloning function was removed in version 5.0.0. Deep cloning of related objects is now the default behavior, and granular control is managed via `attrs` and signals.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Always provide new unique values for unique fields via the `attrs` dictionary when cloning. For example: `clone_instance(original, attrs={'unique_field': 'new_unique_value'})`.","message":"Cloning an instance with `unique=True` fields will raise an `IntegrityError` if the unique field's value is not overridden or set to `None` (if nullable) in the `attrs` argument.","severity":"gotcha","affected_versions":"All"},{"fix":"Review the cloning behavior for related objects. If you need to link to existing related instances instead of creating new ones, or if related objects have unique constraints, you must manage these explicitly via the `attrs` dictionary, setting the ForeignKey/OneToOne field to an existing instance, or handle ManyToMany fields post-cloning. Use the `pre_clone_instance` and `post_clone_instance` signals for advanced control.","message":"By default, `django-clone` attempts to deep clone related objects (ForeignKey, OneToOne, ManyToMany). While powerful, this can lead to unintended duplication if you meant to link to existing related objects or if the related objects themselves have complex unique constraints.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'5.5.0':54 'activ':59 'attribut':46 'clone':2,5,17,49,67 'copi':68 'creation':13 'current':51 'custom':45 'deep':16 'develop':60 'django':1,4,8,20,65 'django-clon':3 'facilit':11 'field':40 'flexibl':43 'foreign':27 'handl':38 'includ':23 'instanc':22 'key':28 'librari':9 'maintain':57 'mani':34,36 'many-to-mani':33 'model':21,66 'object':26 'one':30,32 'one-to-on':29 'overrid':47 'pace':61 'provid':42 'regular':63 'relat':25 'uniqu':39 'updat':64 'util':69 'version':52","created_at":"2026-04-17T01:20:04.997014+00:00","updated_at":"2026-04-17T01:20:04.997014+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"5.5.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/tj-django/django-clone.git","docs":null,"changelog":null,"pypi":"https://pypi.org/project/django-clone/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":null}}