Quickstart

Quickstart#

This page gets you from installation to a working copy.

Installation#

To install with pip:

pip install django-copyist

To install with uv:

uv add django-copyist

django-copyist needs no settings. Add "django_copyist" to INSTALLED_APPS only if you want the copyist_scaffold management command (see Generating and checking configs).

Basic usage#

Assuming you have following models in your Django app:

from django.db import models


class Company(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=100)


class Project(models.Model):
    name = models.CharField(max_length=100)
    company = models.ForeignKey(
        Company, related_name="projects", on_delete=models.CASCADE
    )


class Employee(models.Model):
    name = models.CharField(max_length=100)
    company = models.ForeignKey(
        Company, related_name="employees", on_delete=models.CASCADE
    )


class Counterpart(models.Model):
    name = models.CharField(max_length=100)
    external_id = models.IntegerField()
    project = models.ForeignKey(
        Project, related_name="counterparts", on_delete=models.CASCADE
    )


class Task(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    assignee = models.ForeignKey(
        Employee, related_name="tasks", on_delete=models.CASCADE
    )
    project = models.ForeignKey(Project, related_name="tasks", on_delete=models.CASCADE)
    counterparts = models.ManyToManyField(Counterpart, related_name="tasks")

And you want to create full copy of company with all nested data, but also want it to be created with different name and address. In this case you should write following ModelCopyConfig

from django_copyist import (
    TAKE_FROM_ORIGIN,
    MakeCopy,
    ModelCopyConfig,
    TakeFromInput,
    UpdateToCopied,
)
from example.demo.models import (
    Project,
    Counterpart,
    Task,
    Company,
    Employee,
)


config = ModelCopyConfig(
    model=Company,
    filter_field_to_input_key={"id": "company_id"},
    field_copy_actions={
        "name": TakeFromInput("new_company_name"),
        "address": TakeFromInput("new_company_address"),
        "projects": MakeCopy(
            ModelCopyConfig(
                model=Project,
                field_copy_actions={
                    "name": TAKE_FROM_ORIGIN,
                    "counterparts": MakeCopy(
                        ModelCopyConfig(
                            model=Counterpart,
                            field_copy_actions={
                                "name": TAKE_FROM_ORIGIN,
                                "external_id": TAKE_FROM_ORIGIN,
                            },
                        )
                    ),
                },
            )
        ),
        "employees": MakeCopy(
            ModelCopyConfig(
                model=Employee,
                field_copy_actions={
                    "name": TAKE_FROM_ORIGIN,
                },
            )
        ),
    },
    compound_copy_actions=[
        ModelCopyConfig(
            model=Task,
            field_copy_actions={
                "name": TAKE_FROM_ORIGIN,
                "description": TAKE_FROM_ORIGIN,
                "counterparts": UpdateToCopied(Counterpart),
                "project": UpdateToCopied(Project),
                "assignee": UpdateToCopied(Employee),
            },
        )
    ],
)

Check the config before you run it. check_config() compares it with the model metadata and runs no queries:

from django_copyist import check_config

check_config(config, raise_on_error=True)

Then run the copy:

from django_copyist import run_copy

result = run_copy(
    config,
    input_data={
        "company_id": company_id,
        "new_company_name": new_company_name,
        "new_company_address": new_company_address,
    },
)
assert result.is_copy_successful
new_company_id = result.copies_of(Company)[str(company_id)]

With this, all company data is copied. That seems like a lot to take in, so let’s break it down to what exactly happens here:

  1. We define a ModelCopyConfig for the Company model.

config = ModelCopyConfig(
    model=Company,
    filter_field_to_input_key={"id": "company_id"},
...

ModelCopyConfig is a class that defines how to copy a model. It takes the model class as the first argument and a dictionary that maps the filter field to the input key. This is used to find the object to copy.

  1. Next we define ModelCopyConfig.field_copy_actions for the Company model.

field_copy_actions={
    "name": TakeFromInput("new_company_name"),
    "address": TakeFromInput("new_company_address"),
    "projects": MakeCopy(
        ModelCopyConfig(
            model=Project,
            field_copy_actions={
                "name": TAKE_FROM_ORIGIN,
                "counterparts": MakeCopy(
                    ModelCopyConfig(
                        model=Counterpart,
                        field_copy_actions={
                            "name": TAKE_FROM_ORIGIN,
                            "external_id": TAKE_FROM_ORIGIN,
                        },
                    )
                ),
            },
        )
    ),
    "employees": MakeCopy(
        ModelCopyConfig(
            model=Employee,
            field_copy_actions={
                "name": TAKE_FROM_ORIGIN,
            },
        )
    ),
...

ModelCopyConfig.field_copy_actions is a dictionary that maps the field name to a FieldCopyConfig object.

The FieldCopyConfig object defines how to copy the field. In this case, we take the name and address fields from the input data. TakeFromInput() is a shortcut for a FieldCopyConfig with the TAKE_FROM_INPUT action.

TAKE_FROM_ORIGIN is a shortcut for creating FieldCopyConfig with TAKE_FROM_ORIGIN action, which takes value for new object from original object.

We also define how to copy the projects and employees fields.

We use the MakeCopy action to copy the related objects. MakeCopy is a shortcut for creating FieldCopyConfig with CopyActions.MAKE_COPY action and reference to given model. Nested MakeCopy automatically propagate parent id to child object.

  1. We define compound_copy_actions for the Company model.

compound_copy_actions=[
    ModelCopyConfig(
        model=Task,
        field_copy_actions={
            "name": TAKE_FROM_ORIGIN,
            "description": TAKE_FROM_ORIGIN,
            "counterparts": UpdateToCopied(Counterpart),
            "project": UpdateToCopied(Project),
            "assignee": UpdateToCopied(Employee),
        },
    )
...

compound_copy_actions is a list of ModelCopyConfig objects that define how to copy related objects that are not directly related to the model, or related through multiple relations that need to be created beforehand.

compound_copy_actions are executed after all fields are copied.

In this case, we define how to copy the Task model. We take the name and description fields from the original object. We also define how to copy the counterparts, project, and assignee fields.

UpdateToCopied() is a shortcut for creating FieldCopyConfig with CopyActions.UPDATE_TO_COPIED action and reference to given model. It will search mapping of previously copied objects and update reference to copied object.

  1. We run the copy with run_copy().

result = run_copy(
    config,
    input_data={
        "company_id": company_id,
        "new_company_name": new_company_name,
        "new_company_address": new_company_address,
    },
)

input_data holds the values that root filters and TAKE_FROM_INPUT actions read by key.

run_copy() also accepts:

  • confirm_write: copy even when some references have no match or some rows are ignored. See Closer look at CopyRequest and CopyResult in the overview.

  • using: the database alias for all reads and writes.

  • batch_size: the batch_size of every bulk_create call.

  1. We read the result.

CopyResult tells whether the copy ran (is_copy_successful) and maps every origin primary key to the primary key of its copy:

result.copies_of(Company)  # {"1": "2"}
result.output_map  # {"Company": {"1": "2"}, "Project": {...}, ...}

Keys of output_map, ignored_map and set_to_filter_map are model class names. Primary keys are strings.

If two models in one config share a class name, for example shop.Order and billing.Order, the copy raises ImproperlyConfigured before it runs any query. Key the maps by model label instead:

from django_copyist import MapKeys

result = run_copy(config, input_data={...}, map_keys=MapKeys.LABEL)
result.output_map  # {"shop.Order": {...}, "billing.Order": {...}}

map_keys can also be set on CopyistConfig. Callbacks receive maps with the same keys. result.copies_of(Model) works in both modes.

run_copy() builds a CopyRequest and calls Copyist.execute_copy_request(). You can do the same yourself.

And like this you have copied the company with all related data and can see and edit configuration in one place.

Next steps#