Django Duplicate
Django-Duplicate has an util tool for making a deep copy of django model instances
duplicate.utils.duplicate
Creates and returns a new object that is identical to instance.
DuplicateMixin
Mixin for model classes which implements duplicate function as method.
from django.db import models
from duplicate.models import DuplicateMixin
class Book(DuplicateMixin, models.Model):
title = models.CharField(max_lenght=100)
author = models.ForegnKey('library.Author')
Let's duplicate (shallow copy) book instance:
book = Book(title='Two Scoops of Django')
boook.save()
book_copy = book.duplicate()
If you wanna do deep (related models gonna be shallow copied) copy use paths argument of duplicate method. paths should be list of relation paths:
book_copy = book.duplicate(paths=['author'])
To shallow copy every relation on some level use * symbol:
book_copy = book.duplicate(paths=['*'])
author.* path would copy also all author relations.
There is also symbol ~ which means "reference relations, but if reference object is duplicated in the context use it instead".
You can override fields like this(You cannot override nested m2m fields):
book_copy = book.duplicate(paths=['author'], override_fields={'author.name': 'Overriden author name'})
from duplicate.utils import CallableValue
book_copy = book.duplicate(paths=['author'], override_fields={'author.name': 'Overriden author name'})
You can also override nested fields with callable that takes instace on which you are overriding field as an argument:
from duplicate.utils import CallableValue
book_copy = book.duplicate(
paths=['author'],
override_fields={
'author.name': CallableValue(lambda author: 'Professional author name' if author.professional else 'Author name')
}
)
DJANGO_DUPLICATE_KNOWN_RELATIONS
To ensure that code is aware of all possible to duplicate models and relations path project should have setting DJANGO_DUPLICATE_KNOWN_RELATIONS which is a dict with model labels as keys and list of model relations as value:
DJANGO_DUPLICATE_KNOWN_RELATIONS = {
"duplicate.Author": [
"book_set",
],
"duplicate.Book": [
"author",
],
}
This app enables check which will throw errors when state of app will not correspond to value of DJANGO_DUPLICATE_KNOWN_RELATIONS.
License
The Django Wicked Historian package is licensed under the FreeBSD License.
Release files for django-duplicate 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-duplicate-1.0.0.tar.gz | 11.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_duplicate-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.7 kB
Release files / django-duplicate-1.0.0.tar.gz
| Download URL | django-duplicate-1.0.0.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
99e049914fde9f374555f246115384ed25cd1f82b8f9f52be2d801c01f6421d3
|
|
BLAKE2b-256 checksum How to use checksums |
710730b03f24b3d30923db704e957ff94e7c11a4ca72b70d64ddc8cf09a2244a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|
Release files / django_duplicate-1.0.0-py3-none-any.whl
| Download URL | django_duplicate-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
33abebae2ac39efbc3ea69858d38b6488afab737ab754340ad1be8ba488c0999
|
|
BLAKE2b-256 checksum How to use checksums |
ec9ae2e8f0aba5d38130ba5ae03088eea39cb17602e004f0afb2662b467fb5dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|