Create a clone of a django model instance.
Project description
Python | Django | Downloads | Code Style |
---|---|---|---|
PyPI | Test | Vulnerabilities | Coverage | Code Quality | Pre-Commit |
---|---|---|---|---|---|
|
django-clone
Create copies of a model instance with explicit control on how the instance should be duplicated (limiting fields or related objects copied) with unique field detection.
This solves the problem introduced by using instance.pk = None
and instance.save()
which results in copying more object state than required.
Features
- 100% test coverage.
- More control over how a model instance should be duplicated
- Multi Database support i.e Create duplicates on one or more databases.
- Restrict fields used for creating a duplicate instance.
- Detects unique fields and naively adds a suffix
copy {count}
to each duplicate instance (for supported fields only). - Optionally differentiate between a duplicate instance and the original by appending a copy suffix to non unique fields (for supported fields only).
Table of Contents
- Installation
- Usage
- Advanced Usage
- Compatibility
- Running locally
- Found a Bug?
- Contributors ✨
Installation
pip
pip install django-clone
poetry
poetry add django-clone
Usage
Subclassing the CloneModel
Using the CloneMixin
Duplicating a model instance
Bulk cloning a model
Creating clones without subclassing CloneMixin
.
NOTE: :warning:
- This method won't copy over related objects like Many to Many/One to Many relationships.
- Ensure that required fields skipped from being cloned are passed in using the
attrs
kwargs.
CloneMixin attributes
Attribute | Description |
---|---|
DUPLICATE_SUFFIX |
Suffix to append to duplicates (NOTE: This requires USE_DUPLICATE_SUFFIX_FOR_NON_UNIQUE_FIELDS to be enabled and supports string fields). |
USE_DUPLICATE_SUFFIX_FOR_NON_UNIQUE_FIELDS |
Enable appending the DUPLICATE_SUFFIX to new cloned instances. |
UNIQUE_DUPLICATE_SUFFIX |
Suffix to append to unique fields |
USE_UNIQUE_DUPLICATE_SUFFIX |
Enable appending the UNIQUE_DUPLICATE_SUFFIX to new cloned instances. |
MAX_UNIQUE_DUPLICATE_QUERY_ATTEMPTS |
The max query attempt while generating unique values for a case of unique conflicts. |
Explicit (include only these fields)
Attribute | Description |
---|---|
_clone_fields |
Restrict the list of fields to copy from the instance (By default: Copies all fields excluding auto-created/non editable model fields) |
_clone_m2m_fields |
Restricted Many to many fields (i.e Test.tags) |
_clone_m2o_or_o2m_fields |
Restricted Many to One/One to Many fields |
_clone_o2o_fields |
Restricted One to One fields |
_clone_linked_m2m_fields |
Restricted Many to Many fields that should be linked to the new instance |
Implicit (include all except these fields)
Attribute | Description |
---|---|
_clone_excluded_fields |
Excluded model fields. |
_clone_excluded_m2m_fields |
Excluded many to many fields. |
_clone_excluded_m2o_or_o2m_fields |
Excluded Many to One/One to Many fields. |
_clone_excluded_o2o_fields |
Excluded one to one fields. |
NOTE: :warning:
- Ensure to either set
_clone_excluded_*
or_clone_*
. Using both would raise errors.
Django Admin
Duplicating Models from the Django Admin view.
List View
Change View
CloneModelAdmin class attributes
NOTE: :warning:
- Ensure that
model_clone
is placed beforedjango.contrib.admin
INSTALLED_APPS = [
'model_clone',
'django.contrib.admin',
'...',
]
Advanced Usage
Signals
pre_clone_save, post_clone_save
Clone Many to Many fields
Using the CloneModel
Using the CloneMixin
Multi database support
Compatibility
Python | Supported version |
---|---|
Python2.x | <=2.5.3 |
Python3.5 | <=2.9.6 |
Python3.6+ | All versions |
Django | Supported version |
---|---|
1.11 | <=2.7.2 |
2.x | All versions |
3.x | All versions |
Running locally
$ git clone git@github.com:tj-django/django-clone.git
$ make default-user
$ make run
Spins up a django server running the demo app.
Visit http://127.0.0.1:8000
Found a Bug?
To file a bug or submit a patch, please head over to django-clone on github.
If you feel generous and want to show some extra appreciation:
Support me with a :star:
Contributors ✨
Thanks goes to these wonderful people:
Gerben Neven 🐛 ⚠️ 💻 |
Sebastian Kapunkt 💻 🐛 ⚠️ |
Andrés Portillo 🐛 |
WhiteSource Renovate 🚧 |
Yuekui 💻 🐛 ⚠️ 📖 🚧 |
Take Weiland ⚠️ 🐛 💻 |
Patrick 🐛 💻 |
Amiel Kollek 💻 🐛 ⚠️ |
Eric Theise 📖 |
Daniel Schaffer 💻 ⚠️ |
This project follows the all-contributors specification. Contributions of any kind welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-clone-5.3.3.tar.gz
.
File metadata
- Download URL: django-clone-5.3.3.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7521798c6bcdea3168fce79d4857afc1d02e02e8e4d801585ad8a36a19d16cc8 |
|
MD5 | f79ae2c143ce202fd88b8e49baf95da8 |
|
BLAKE2b-256 | 3c6c4b9388069099401f2b9086707e583a8e09acda40e4bc330fd21cb41ecf42 |
File details
Details for the file django_clone-5.3.3-py3-none-any.whl
.
File metadata
- Download URL: django_clone-5.3.3-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea2e5a770d01c62ecf66d0dd6e8df284e26321245b8abcc6d82d8740765b42d5 |
|
MD5 | ef0f4f17be09aeb900d4c96473e62fc5 |
|
BLAKE2b-256 | 2b5e2868bede7b6ed783bf08da3bc062c6c77ebe881b0e46009510a91284a152 |