Skip to main content

Create a clone of a django model instance.

Project description

CircleCI PyPI - Python Version PyPI - License PyPI - Django Version


Creating clones (i.e a copy) of a model instance on the fly.

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 _(

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

    _clonable_many_to_many_fields = ['tags']

Creating a clone

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.title
Out[5]: 'New'

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

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

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

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-clone, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size django_clone-0.0.2-py3-none-any.whl (6.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size django-clone-0.0.2.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page