Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

SYSCON-International/django-clone

 
 

Repository files navigation

CircleCI 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.

Using class 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.

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

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']

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>]>

About

Cloning django model instances.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 79.3%
  • Makefile 20.7%