django-copyist
Deep-copy Django model instances with all their related objects, in a constant number of queries.
django-copyist copies a tree of Django models: a root object, its one-to-many children, their children, and many-to-many links. You describe the copy in a declarative config. The models stay untouched. Each level of the tree is read with IN lookups and written with bulk_create, so the query count depends on the depth of the tree, not on the number of rows.
On PostgreSQL, a copy of 160 000 rows takes 14 queries and 3.5 s with django-copyist, against 400 000 queries and 83 s with per-row cloning (benchmark).
Install
pip install django-copyist
Requires Python 3.11+ and Django 5.2, 6.0 or 6.1. CI runs on SQLite and on PostgreSQL 14, 15 and 18.
Example
Copy a company with its projects and employees, and give the copy a new name:
from django_copyist import TAKE_FROM_ORIGIN, MakeCopy, ModelCopyConfig, TakeFromInput, run_copy
from myapp.models import Company, Employee, Project
COMPANY_COPY_CONFIG = ModelCopyConfig(
model=Company,
# Selects the root row: Company.objects.filter(id=input_data["company_id"])
filter_field_to_input_key={"id": "company_id"},
field_copy_actions={
"name": TakeFromInput("new_name"),
"address": TAKE_FROM_ORIGIN,
# One-to-many children. The copies point to the new company.
"projects": MakeCopy(
ModelCopyConfig(model=Project, field_copy_actions={"name": TAKE_FROM_ORIGIN})
),
"employees": MakeCopy(
ModelCopyConfig(model=Employee, field_copy_actions={"name": TAKE_FROM_ORIGIN})
),
},
)
result = run_copy(
COMPANY_COPY_CONFIG,
input_data={"company_id": company.pk, "new_name": "Acme (copy)"},
)
assert result.is_copy_successful
new_company = Company.objects.get(pk=result.copies_of(Company)[str(company.pk)])
Fields that are not in field_copy_actions get their default value. Every copy runs in one transaction.
Generate and check configs
Add "django_copyist" to INSTALLED_APPS, then print a starting config for any model:
python manage.py copyist_scaffold myapp.Company --depth 2
Check every config in your test suite. check_config runs no queries. It reports unknown fields, required fields that are not copied, unique fields copied from the origin, unsupported model shapes and more:
from django_copyist import check_config
def test_company_copy_config():
check_config(COMPANY_COPY_CONFIG, raise_on_error=True)
What else it does
Remap references to existing rows (SET_TO_FILTER): copy tasks into another project and point each task at the matching counterpart in that project, found by your filter or function.
Dry run, then confirm: when some references have no match, the copy stops and returns what did not match and what would be skipped. Show it to the user, then run again with confirm_write=True. The copy aborts if the data changed in the meantime.
Diamond-shaped trees (compound_copy_actions): copy a model that points to several copied parents, for example a task that belongs to a copied project and a copied employee.
Pre-copy and post-copy steps: delete old data in the target or run your own function with the map of origin to copy primary keys.
Database alias and batch size: run_copy(..., using="replica_rw", batch_size=500).
When to use it
Use django-copyist to copy whole trees: project templates, tenant data, document versions, scenario forks. It fits when the tree is large, when copies must point at rows in a different parent, or when the user must confirm what will be copied.
Use django-clone to duplicate a single object, for example from a Django admin action. To copy related objects, django-clone needs its mixin on every model in the tree. It copies row by row, so it calls save() and fires model signals.
django-copyist |
django-clone |
|
|---|---|---|
Configuration |
Config object, models unchanged |
Mixin and attributes on each model in the tree |
Queries for a tree of N rows |
Constant per tree level |
Grows with N |
Point copies at matching rows of another parent |
Yes (SET_TO_FILTER) |
No built-in matching |
Dry run and confirmation |
Yes |
No |
Django admin action |
No |
Yes |
save() and post_save signals |
Not called (bulk_create) |
Called |
Limitations
Uses bulk_create: save() and pre_save/post_save signals do not run.
The database must return primary keys from bulk_create (PostgreSQL, SQLite 3.35+, MariaDB), unless the model sets its primary key in Python, for example UUIDField(default=uuid4). MySQL and Oracle are not supported for auto-increment keys. MariaDB is not tested in CI.
No multi-table inheritance, no reverse one-to-one copies, no self-referencing trees (a model with a foreign key to itself) yet. check_config reports all of these.
See all limitations.
Documentation
llms.txt for AI coding assistants
Metadata
Release files for django-copyist 0.2.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 | Provenance | Actions |
|---|---|---|---|---|
| django_copyist-0.2.0.tar.gz | 36.4 kB | With provenance | Details for django_copyist-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_copyist-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_copyist-0.2.0-py3-none-any.whl |
Total release size: 74.6 kB
Release files / django_copyist-0.2.0.tar.gz
| Download URL | django_copyist-0.2.0.tar.gz |
|---|---|
| Size | 36.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b8b1e0ec4851849cfc8d0339adafa019cb6e26bb530cab8173bcf6aa5d31aac7
|
|
BLAKE2b-256 checksum How to use checksums |
6396c5a4592a1d3a8517e8671031ff5f81656d5ebe2a7e39532fc05cf42635f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logRelease files / django_copyist-0.2.0-py3-none-any.whl
| Download URL | django_copyist-0.2.0-py3-none-any.whl |
|---|---|
| Size | 38.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
124adb75dd0152f984325596db19ec2f4818436892dcac125466f2310d9a15fe
|
|
BLAKE2b-256 checksum How to use checksums |
a15df0f9de7d7aa71133d79bcfce99b8bcb839b05458dd80d0411d0f22cb70b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logSecurity for django-copyist 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for django_copyist-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.