• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Controlled Django model instance replication.

4FC889E9-FF59-4E44-9EB6-2AF7DC034C74

Python Django Downloads Code Style
PyPI - Python Version PyPI - Django Version Downloads Code style: black
PyPI Test Vulnerabilities Coverage Code Quality Pre-Commit
PyPI version Test Known Vulnerabilities Codacy Badge
codecov
Codacy Badge pre-commit.ci status

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

pip

pip install django-clone

poetry

poetry add django-clone

Usage

Subclassing the CloneModel

Using the CloneMixin

Using the CloneModel

Duplicating a model instance

Bulk cloning a model

Creating clones without subclassing CloneMixin.

NOTE: โš ๏ธ

  • 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: โš ๏ธ

  • Ensure to either set _clone_excluded_* or _clone_*. Using both would raise errors.

Django Admin

Duplicating Models from the Django Admin view.

List View

Screenshot

Change View

Screenshot

CloneModelAdmin class attributes

NOTE: โš ๏ธ

  • Ensure that model_clone is placed before django.contrib.admin
INSTALLED_APPS = [
    'model_clone',
    'django.contrib.admin',
    '...',
]

Advanced Usage

Signals

pre_clone_save, post_clone_save

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 [email protected]: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 โญ

Buy me a coffee

Contributors โœจ

Thanks goes to these wonderful people:

Gerben Neven
Gerben Neven

๐Ÿ› โš ๏ธ ๐Ÿ’ป
Sebastian Kapunkt
Sebastian Kapunkt

๐Ÿ’ป ๐Ÿ› โš ๏ธ
Andrรฉs Portillo
Andrรฉs Portillo

๐Ÿ›
WhiteSource Renovate
WhiteSource Renovate

๐Ÿšง
Yuekui
Yuekui

๐Ÿ’ป ๐Ÿ› โš ๏ธ ๐Ÿ“– ๐Ÿšง
Take Weiland
Take Weiland

โš ๏ธ ๐Ÿ› ๐Ÿ’ป
Patrick
Patrick

๐Ÿ› ๐Ÿ’ป
Amiel Kollek
Amiel Kollek

๐Ÿ’ป ๐Ÿ› โš ๏ธ
Eric Theise
Eric Theise

๐Ÿ“–
Daniel Schaffer
Daniel Schaffer

๐Ÿ’ป โš ๏ธ

This project follows the all-contributors specification. Contributions of any kind welcome!