Create a clone of a django model instance.
Project description
django-clone
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 _(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>]>
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.2.tar.gz
(5.1 kB
view details)
Built Distribution
File details
Details for the file django-clone-0.0.2.tar.gz
.
File metadata
- Download URL: django-clone-0.0.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3794ce0b1557fc86470ebc04963818cf249d7956862aa1bf3de3724e2611584 |
|
MD5 | 1823310c8faf234ad77dd42cea99d915 |
|
BLAKE2b-256 | c886a8647f5cfbec42b43533f87782ff1e2ff4832bc14d268d7d85772e14d0a6 |
File details
Details for the file django_clone-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: django_clone-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b551e6ae7f0b7decd40c25e16d15486b12dcf5e1b2da069b92d689fdd7d7d83 |
|
MD5 | 16e30a33328bea0bd2653f55e3024868 |
|
BLAKE2b-256 | b0de3438dc68f3fb1b922466d9324fd0f5ea8317d34d3570569aab5e5924c0a2 |