Skip to main content

Create a clone of a django model instance.

Project description

CircleCI All Contributors PyPI - Python Version PyPI - License PyPI - Django Version

django-clone

Creating copies of a model instance on the fly offering more control on how the object should be cloned with support for limiting the fields or related objects copied.

Table of contents

Installation

pip install django-clone

Add model_clone to your INSTALLED_APPS

INSTALLED_APPS = [
    ...
    'model_clone',
    ...
]

Usage

from django.db import models
from django.utils.translation import gettext_lazy as _
from model_clone import CloneMixin

class Tags(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return _(self.name)


class TestModel(CloneMixin, models.Model):
    title = models.CharField(max_length=200)
    tags =  models.ManyToManyField(Tags)

    _clonable_many_to_many_fields = ['tags']

Duplicating a model instance

In [1]: test_obj = TestModel.objects.create(title='New')

In [2]: test_obj.tags.create(name='men')

In [3]: test_obj.tags.create(name='women')

In [4]: clone = test_obj.make_clone(attrs={'title': 'Updated title'})

In [5]: test_obj.pk
Out[5]: 1

In [6]: test_obj.title
Out[6]: 'New'

In [7]: test_obj.tags.all()
Out[7]: <QuerySet [<Tag: men>, <Tag: women>]>

In [8]: clone.pk
Out[8]: 2

In [9]: clone.title
Out[9]: 'Updated title'

In [10]: clone.tags.all()
Out[10]: <QuerySet [<Tag: men>, <Tag: women>]>

CloneMixin attributes

_clonable_model_fields: Restrict the list of fields to copy from the instance.
_clonable_many_to_many_fields: Restricted Many to many fields (i.e Test.tags).
_clonable_many_to_one_or_one_to_many_fields: Restricted Many to One/One to Many fields.
_clonable_one_to_one_fields: Restricted One to One fields.

Creating clones without subclassing CloneMixin.

:warning: This method won't copy over related objects like Many to Many/One to Many relationships.

:warning: Ensure that required fields skipped from being cloned are passed in using the attrs dictionary.

In [1]: from model_clone import create_copy_of_instance

In [2]: test_obj = TestModel.objects.create(title='New')

In [3]: test_obj.tags.create(name='men')

In [4]: test_obj.tags.create(name='women')

In [5]: clone = create_copy_of_instance(test_obj, attrs={'title': 'Updated title'})

In [6]: test_obj.pk
Out[6]: 1

In [7]: test_obj.title
Out[7]: 'New'

In [8]: test_obj.tags.all()
Out[8]: <QuerySet [<Tag: men>, <Tag: women>]>

In [9]: clone.pk
Out[9]: 2

In [10]: clone.title
Out[10]: 'Updated title'

In [11]: clone.tags.all()
Out[11]: <QuerySet []>

Duplicating Models from Django Admin view.

Change

from django.contrib import admin
from django.contrib.admin import ModelAdmin

@admin.register(TestModel)
class ModelToCloneAdmin(ModelAdmin):
    pass

to

from model_clone import ClonableModelAdmin

@admin.register(TestModel)
class ModelToCloneAdmin(ClonableModelAdmin):
    pass

List View

Screenshot

Change View

Screenshot

SETTINGS

include_duplicate_action: Enables/Disables the Duplicate action in the List view (Defaults to True) include_duplicate_object_link: Enables/Disables the Duplicate action in the Change view (Defaults to True)

:warning: Ensure that model_clone is placed before django.contrib.admin

INSTALLED_APPS = [
    'model_clone',
    'django.contrib.admin',
    '...',
]

Contributors ✨

Thanks goes to these wonderful people:

Gerben Neven
Gerben Neven

🐛 ⚠️ 💻
Sebastian Kapunkt
Sebastian Kapunkt

💻 🐛
Andrés Portillo
Andrés Portillo

🐛

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

django-clone-0.0.8.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

django_clone-0.0.8-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file django-clone-0.0.8.tar.gz.

File metadata

  • Download URL: django-clone-0.0.8.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.4

File hashes

Hashes for django-clone-0.0.8.tar.gz
Algorithm Hash digest
SHA256 c8a365811e75ba7fda44cb00de387bcd25e075c026c657e231fc44df0bab678c
MD5 0e144452386039bb512e27b174d86fa7
BLAKE2b-256 b3deef3bca312e06134626d6f4155f43fdb3395f3e96f55762bb0cdc9fce3953

See more details on using hashes here.

File details

Details for the file django_clone-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: django_clone-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.4

File hashes

Hashes for django_clone-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2e99895b125749f7df93e588f4dce0d6e512119f87bb36bbd49be83137139d
MD5 562c935bd96ab320d0e0ee9d8663635e
BLAKE2b-256 f8246059d35eba8f59301e586cc7abbfb8e5e684d4a5b768337c49153f432d63

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page