Giter Site home page Giter Site logo

django-clone's Introduction

PyPI Python Django LICENSE
PyPI version PyPI - Python Version PyPI - Django Version PyPI - License
Test Vulnerabilities Coverage Code Quality Contributors
CircleCI Known Vulnerabilities Codacy Badge Codacy Badge All Contributors

django-clone

Creating copies of a model instance with explicit control on how the instance should be duplicated (limiting fields or related objects) copied and unique field detection.

Table of contents

Installation

pip install django-clone

Usage

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

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

    _clone_many_to_many_fields = ['tags']
    

class Tags(models.Model):
    name = models.CharField(max_length=255)
    
    def __str__(self):
        return _(self.name)

Duplicating a model instance

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

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

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

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

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

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

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

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

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

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

CloneMixin attributes

Explicit
Field Names Description
_clone_model_fields Restrict the list of fields to copy from the instance (By default: Copies all fields excluding auto-created/non editable model fields)
_clone_many_to_many_fields Restricted Many to many fields (i.e Test.tags)
_clone_many_to_one_or_one_to_many_fields Restricted Many to One/One to Many fields
_clone_one_to_one_fields Restricted One to One fields
Implicit
Field Names (include all except these fields.) Description
_clone_excluded_model_fields Excluded model fields.
_clone_excluded_many_to_many_fields Excluded many to many fields.
_clone_excluded_many_to_one_or_one_to_many_fields Excluded Many to One/One to Many fields.
_clone_excluded_one_to_one_fields Excluded one to one fields.

⚠️ NOTE: Ensure to either set _clone_excluded_* or _clone_*. Using both would raise errors.

Creating clones without subclassing CloneMixin.

In [1]: from model_clone import create_copy_of_instance

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

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

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

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

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

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

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

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

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

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

⚠️ 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 dictionary.

Duplicating Models from Django Admin view.

Change

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

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

to

from model_clone import CloneModelAdmin

@admin.register(TestModel)
class TestModelAdmin(CloneModelAdmin):
    pass

List View

Screenshot

Change View

Screenshot

CLONE MODEL ADMIN CLASS PROPERTIES
from model_clone import CloneModelAdmin

@admin.register(TestModel)
class TestModelAdmin(CloneModelAdmin):
    # Enables/Disables the Duplicate action in the List view (Defaults to True)
    include_duplicate_action = True
    # Enables/Disables the Duplicate action in the Change view (Defaults to True)
    include_duplicate_object_link = True

⚠️ NOTE: Ensure that model_clone is placed before django.contrib.admin

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

Found a Bug?

To file a bug or submit a patch, please head over to django-clone on github.

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!

django-clone's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.