Registry / web-framework / django-clone

django-clone

JSON →
library5.5.0pypypi✓ verified 89d ago

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.

pip install django-clone
INSTALL
IMPORT
SIG · DJANGO-CLONE
D
django-clone
web-frameworkpythonv5.5.0
Install
3.6s avg
Import
—
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v5.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.6MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 3.6s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CloneMixin
✓ from model_clone import CloneMixin
✗ from django_clone import clone_instance
create_copy_of_instance
✓ from model_clone import create_copy_of_instance
✗ from django_clone import clone_instance
CloneModelAdmin
✓ from model_clone import CloneModelAdmin
✗ from django_clone import clone_instance

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()`.

import os from django.db import models from django_clone import clone_instance # Minimal Django setup for runnable example os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') import django django.setup() class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=100) isbn = models.CharField(max_length=13, unique=True, null=True, blank=True) description = models.TextField(blank=True) author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books') def __str__(self): return self.title # Create some initial instances author1 = Author.objects.create(name="Jane Doe") original_book = Book.objects.create( title="The Original Story", isbn="1234567890123", description="A timeless classic.", author=author1 ) # Clone the book instance, providing a new unique ISBN cloned_book = clone_instance(original_book, attrs={'isbn': '9876543210987', 'title': 'The Cloned Story'}) print(f"Original Book: {original_book.title} (ISBN: {original_book.isbn}, Author: {original_book.author.name})") print(f"Cloned Book: {cloned_book.title} (ISBN: {cloned_book.isbn}, Author: {cloned_book.author.name})")
Debug
Known issues
breakingThe 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.
fix
Update your imports from `from django_clone.utils import clone_model_instance` to `from django_clone import clone_instance`.
affects: >=5.0.0
breakingThe `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.
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.
affects: >=5.0.0
gotchaCloning 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.
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'})`.
affects: All
gotchaBy 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.
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.
affects: All
Upgrade
Version history
5.5.0latest on PyPI · released Jun 10, 2025
Audit
Dependencies
DjangorequiredCore framework dependency for any Django library.
django-mpttoptionalRequired only if cloning MPTT models to preserve tree structure during cloning.
Agent activity
6 hits · last 30 days
node
6
Resources
django-clone — pip install django-clone · libregistry