API#

Import everything from the package root. Names listed here are public API and follow semantic versioning. Other names in the submodules may change in a minor release.

from django_copyist import ModelCopyConfig, MakeCopy, TAKE_FROM_ORIGIN, run_copy

Running a copy#

django_copyist.run_copy(config: ModelCopyConfig | CopyistConfig, input_data: dict[str, Any], *, confirm_write: bool = False, set_to_filter_map: dict[str, dict[str, dict[str, str | None]]] | None = None, ignored_map: dict[str, list[Any]] | None = None, using: str | None = None, batch_size: int | None = None, map_keys: MapKeys | None = None) → CopyResult#

Validate and execute a copy in one call.

This is a shortcut for building a CopyRequest and calling Copyist.execute_copy_request().

Parameters:
  • config – One root ModelCopyConfig, or a CopyistConfig with several roots.

  • input_data – Values that root filters, TAKE_FROM_INPUT actions and FROM_INPUT filters read by key.

  • confirm_write – Copy even when SET_TO_FILTER has unmatched values or some instances are ignored. Pass the maps from the previous result to make sure the data did not change since the user saw them.

  • set_to_filter_map – set_to_filter_map from the previous, aborted result.

  • ignored_map – ignored_map from the previous, aborted result.

  • using – Database alias for all reads and writes. Defaults to the router choice.

  • batch_size – batch_size for every bulk_create call.

  • map_keys – Key mode of the result maps. Overrides CopyistConfig.map_keys. Defaults to MapKeys.MODEL_NAME ("Company"); use MapKeys.LABEL ("demo.Company") when two copied models share a class name.

Returns:

The CopyResult. Check is_copy_successful before reading output_map.

class django_copyist.copy_request.CopyRequest(input_data: dict[str, Any], config: CopyistConfig, confirm_write: bool = False, set_to_filter_map: ForwardRef('SetToFilterMap') | None = None, ignored_map: ForwardRef('IgnoredMap') | None = None, using: str | None = None, batch_size: int | None = None)#

This is the base class for a copy request, which serves as the input for the Copyist.

Parameters:
  • input_data (dict[str, Any]) – The input data used to determine which models should be copied. It can also contain additional data that will be utilized during the copying process.

  • config (CopyistConfig) – An instance of CopyistConfig that holds all the necessary settings for the copying operation.

  • confirm_write (bool, optional) – A flag indicating whether the copy operation should proceed even if there are unmatched values in the models or some models are ignored, defaults to False.

  • set_to_filter_map (SetToFilterMap, optional) – A dictionary from the previous copy result that holds substitute data. If provided, the Copyist will compare it with the new set_to_filter_map to ensure that the data hasn’t changed since the last copy attempt. If the data has changed, the Copyist will return a CopyResult with ‘is_copy_successful’ set to False and an updated set_to_filter_map, even if ‘confirm_write’ is True, defaults to None.

  • ignored_map (IgnoredMap, optional) – A dictionary from the previous copy result that holds data of ignored models. If provided, the Copyist will compare it with the new ignored_map to ensure that the data hasn’t changed since the last copy attempt. If the data has changed, the Copyist will return a CopyResult with ‘is_copy_successful’ set to False and an updated ignored_map, even if ‘confirm_write’ is True, defaults to None.

  • using (str, optional) – Database alias for every read and write of the copy. Defaults to the database router choice for each model, defaults to None.

  • batch_size (int, optional) – batch_size for every bulk_create call, defaults to None (one statement per model and level, split only where the backend requires it).

class django_copyist.copy_request.CopyResult(is_copy_successful: bool, output_map: ForwardRef('OutputMap') | None, ignored_map: IgnoredMap, set_to_filter_map: SetToFilterMap, reason: ~django_copyist.copy_request.AbortReason | None = None, map_keys: ~django_copyist.config.MapKeys = MapKeys.MODEL_NAME, model_labels: dict[str, str] = <factory>)#

This is the base class for a copy result, which serves as the output for the Copyist.

Variables:
  • is_copy_successful – A flag indicating whether the copy operation was successful, defaults to False.

  • output_map – Maps model keys to dictionaries of origin primary key to copy primary key. Primary keys are strings. A model key is the class name ("Company"), or the label ("demo.Company") with MapKeys.LABEL. None if the copy did not run. Use copies_of() to read it by model class.

  • ignored_map – Maps model keys to lists of primary keys of origin rows that were ignored during the copying process.

  • set_to_filter_map –

    A dictionary that contains data of substitutes matched by the SET_TO_FILTER action. The structure is as follows:

    {
        "ModelName": {
            "field_name": {
                "origin_pk": "substitute_pk" | None
            }
        }
    }
    

    Defaults to None.

  • reason – The reason code, returned if is_copy_successful is False, defaults to None.

  • map_keys – The key mode of the maps, from CopyistConfig.map_keys.

  • model_labels – Maps every map key to the label of its model, for example {"Company": "demo.Company"}.

copies_of(model: type[Model]) → dict[str, str]#

Map of origin primary key to copy primary key for one model.

Parameters:

model – Model class.

Returns:

{"<origin pk>": "<copy pk>"}. Empty if the model was not copied or the copy did not run.

class django_copyist.copy_request.AbortReason(*values)#
DATA_CHANGED_IGNORED = 'DATA_CHANGED_IGNORED'#
DATA_CHANGED_STF = 'DATA_CHANGED_STF'#
IGNORED = 'IGNORED'#
NOT_MATCHED = 'NOT_MATCHED'#
class django_copyist.copyist.Copyist(copy_request: CopyRequest)#

The main class responsible for copying Django model instances based on the provided configuration.

Parameters:

copy_request (CopyRequest) – The copy request object containing the configuration and input data for the copy operation.

Variables:
  • request (CopyRequest) – The copy request object containing the configuration and input data for the copy operation.

  • config (CopyistConfig) – The configuration for the copy operation.

  • input_data (dict) – The input data for the copy operation.

class django_copyist.copyist.CopyistConfig(model_configs: list[ModelCopyConfig], map_keys: MapKeys = MapKeys.MODEL_NAME)#

This is the root copy configuration that contains a list of ModelCopyConfig.

Variables:
  • model_configs (list[ModelCopyConfig]) – The list of model configurations.

  • map_keys (MapKeys) – How models are keyed in output_map, ignored_map and set_to_filter_map. MODEL_NAME (default) uses the class name, for example "Company". If two models in the config share a class name, the copy raises ImproperlyConfigured: switch to LABEL ("demo.Company").

class django_copyist.copyist.CopyIntent(origin: ~django.db.models.base.Model, copy_data: dict[str, ~typing.Any] = <factory>, m2m_copy_intent_list: list[~django_copyist.copyist.M2MCopyIntent] = <factory>, copied: ~django.db.models.base.Model | None = None)#

Intermediate model storing intent for copying data for instance of model

Configuration#

enum django_copyist.config.CopyActions(value)#
Member Type:

str

Valid values are as follows:

TAKE_FROM_ORIGIN = <CopyActions.TAKE_FROM_ORIGIN: 'TAKE_FROM_ORIGIN'>#
TAKE_FROM_INPUT = <CopyActions.TAKE_FROM_INPUT: 'TAKE_FROM_INPUT'>#
MAKE_COPY = <CopyActions.MAKE_COPY: 'MAKE_COPY'>#
UPDATE_TO_COPIED = <CopyActions.UPDATE_TO_COPIED: 'UPDATE_TO_COPIED'>#
SET_TO_FILTER = <CopyActions.SET_TO_FILTER: 'SET_TO_FILTER'>#
enum django_copyist.config.DataModificationActions(value)#
Member Type:

str

Valid values are as follows:

DELETE_BY_FILTER = <DataModificationActions.DELETE_BY_FILTER: 'DELETE_BY_FILTER'>#
EXECUTE_FUNC = <DataModificationActions.EXECUTE_FUNC: 'EXECUTE_FUNC'>#
protocol django_copyist.config.DataPreparationFunc#

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

__annotations_cache__ = {}#
__call__(*, model_config: ModelCopyConfig, input_data: dict[str, Any], set_to_filter_map: SetToFilterMap, output_map: OutputMap) → None#

A protocol that defines a function to be used for preparing data before the copying process.

This function is expected to take several parameters including the model configuration, input data, set to filter map, and output map. It does not return any value.

Parameters:
  • model_config (ModelCopyConfig) – The configuration for the model being copied.

  • input_data (Dict[str, Any]) – The input data for the copy request.

  • set_to_filter_map (SetToFilterMap) – The current global set to filter map.

  • output_map (OutputMap) – The current global output map.

class django_copyist.config.DataPreparationStep(action: django_copyist.config.DataModificationActions, filter_field_to_input_key: dict[str, str] | None = None, func: django_copyist.config.DataPreparationFunc | None = None)#
class django_copyist.config.FieldCopyConfig(action: CopyActions, copy_with_config: ModelCopyConfig | None = None, reference_to: type[Model] | None = None, filter_config: FilterConfig | None = None, input_key: str | None = None)#

This class is a configuration that describes how a specific field should be copied.

Parameters:
  • action (CopyAction) – The action to be executed.

  • copy_with_config (ModelCopyConfig, optional) – A nested instance of ModelCopyConfig. This should be defined if the action is MAKE_COPY. It describes how a nested model should be copied, defaults to None.

  • reference_to (Model, optional) – The model type to which the field should be updated. This should be defined if the action is UPDATE_TO_COPIED or SET_TO_FILTER, defaults to None.

  • filter_config (FilterConfig, optional) – An instance of FilterConfig. This should be defined if the action is SET_TO_FILTER. It describes how the value for the field should be queried from existing data, defaults to None.

  • input_key (str, optional) – The key in the input data from which the value for the field should be taken. This should be defined if the action is TAKE_FROM_INPUT, defaults to None.

Raises:

ValueError – If the action is MAKE_COPY and copy_with_config is not defined, or if the action is UPDATE_TO_COPIED and reference_to is not defined, or if the action is SET_TO_FILTER and either filter_config or reference_to is not defined, or if the action is TAKE_FROM_INPUT and input_key is not defined.

class django_copyist.config.FieldFilterConfig(source: FilterSource, key: str | None = None)#

This configuration specifies the source from which the filter value is obtained.

If the source is FROM_ORIGIN, the filter value is derived from the original model. If the source is FROM_INPUT, the filter value is extracted from the input data.

Parameters:
  • source (FilterSource) – A FilterSource enumeration value. It can be either FROM_INPUT or FROM_ORIGIN.

  • key (str, optional) – An optional key in the input data that identifies the filter value. This should be specified if the source is FROM_INPUT, defaults to None.

class django_copyist.config.FilterConfig(filters: dict[str, FieldFilterConfig] | None = None, filter_func: SetToFilterFunc | None = None)#

This class is a configuration that describes how a field’s value should be queried from existing data.

Parameters:
  • filters (Dict[str, FieldFilterConfig], optional) – A dictionary storing information on how a field should be queried. The key is the field name, and the value is a FieldFilterConfig instance. If a field is not present in filters, it will be ignored and the default for the model will be used, defaults to None.

  • filter_func (SetToFilterFunc, optional) – An optional instance of SetToFilterFunc. If present, it will be used for querying the field value from existing data, defaults to None.

enum django_copyist.config.FilterSource(value)#
Member Type:

str

Valid values are as follows:

FROM_INPUT = <FilterSource.FROM_INPUT: 'FROM_INPUT'>#
FROM_ORIGIN = <FilterSource.FROM_ORIGIN: 'FROM_ORIGIN'>#
class django_copyist.config.IgnoreCondition(filter_conditions: list[IgnoreFilter] | None = None, ignore_func: IgnoreFunc | None = None)#

This configuration describes the conditions under which the copying of a model should be disregarded.

Either ‘filter_conditions’ or ‘ignore_func’ must be specified.

Attributes:
filter_conditions: An optional list of IgnoreFilter instances.

If provided, these will be used to filter out models that should not be copied.

ignore_func: A function that, when provided, should return a

list of models that are to be excluded from copying.

class django_copyist.config.IgnoreFilter(filter_name: str, set_to_filter_origin_model: type[Model], set_to_filter_field_name: str, filter_source: IgnoreFilterSource = IgnoreFilterSource.UNMATCHED_SET_TO_FILTER_VALUES)#

This configuration describes a filter for excluding certain models during the copying process.

It is applicable when you wish to exclude a model from being copied if some of its fields do not match any existing data. This is particularly useful when using the SET_TO_FILTER action.

Parameters:
  • filter_name (str) – The name of the filter field that will be used to exclude all models that do not match any existing data. Usually is an __in filter, as it will be given list of unmatched ids to exclude. e.g. if you have hierarchy Parent -> Child <-M2M-> Tag and want to ignore all parents, where child didn’t match any of its tags, you can use child_set__tags__id__in

  • filter_source (IgnoreFilterSource) – The value of the IgnoreFilterSource enum. It is always set to UNMATCHED_SET_TO_FILTER_VALUES.

  • set_to_filter_origin_model (Type[Model]) – The type of the model in which unmatched fields for the SET_TO_FILTER action are expected.

  • set_to_filter_field_name (str) – The name of the field for which unmatched values are expected.

enum django_copyist.config.IgnoreFilterSource(value)#
Member Type:

str

Valid values are as follows:

UNMATCHED_SET_TO_FILTER_VALUES = <IgnoreFilterSource.UNMATCHED_SET_TO_FILTER_VALUES: 'UNMATCHED_SET_TO_FILTER_VALUES'>#
protocol django_copyist.config.IgnoreFunc#

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

__annotations_cache__ = {}#
__call__(*, model_config: ModelCopyConfig, set_to_filter_map: SetToFilterMap, model_extra_filter: Q | None, ignored_map: IgnoredMap, input_data: dict[str, Any]) → list[Model]#

A protocol that defines a function to be used for ignoring certain models during the copying process.

This function is expected to take several parameters including the model configuration, set to filter map, model extra filter, ignored map, and input data. It should return a list of models that are to be excluded from copying.

Parameters:
  • model_config (ModelCopyConfig) – The configuration for the model being copied.

  • set_to_filter_map (SetToFilterMap) – The current global set to filter map.

  • model_extra_filter (Q, optional) – An optional Q instance, representing current filters for model. It is generated by copyist during copy process (e.g. to filter instance list by parent id). You should apply it to your query to narrow down the list of models you are working with.

  • ignored_map (IgnoredMap) – The current global ignored map.

  • input_data (Dict[str, Any]) – The input data for the copy request.

Returns:

The list of models that are to be excluded from copying.

Return type:

List[Model]

django_copyist.config.MakeCopy(config: ModelCopyConfig) → FieldCopyConfig#

This function is a shortcut for creating a FieldCopyConfig instance with the MAKE_COPY action.

Parameters:

config (ModelCopyConfig) – A nested instance of ModelCopyConfig. This describes how a nested model should be copied.

Returns:

A FieldCopyConfig instance with the action set to MAKE_COPY and the copy_with_config set to the provided config.

Return type:

FieldCopyConfig

enum django_copyist.config.MapKeys(value)#

How models are keyed in output_map, ignored_map and set_to_filter_map.

Member Type:

str

Valid values are as follows:

MODEL_NAME = <MapKeys.MODEL_NAME: 'MODEL_NAME'>#
LABEL = <MapKeys.LABEL: 'LABEL'>#
class django_copyist.config.ModelCopyConfig(model: type[~django.db.models.base.Model], field_copy_actions: dict[str, ~django_copyist.config.FieldCopyConfig], ignore_condition: ~django_copyist.config.IgnoreCondition | None = None, compound_copy_actions: list[~django_copyist.config.ModelCopyConfig] = <factory>, filter_field_to_input_key: dict[str, str] = <factory>, data_preparation_steps: list[~django_copyist.config.DataPreparationStep] = <factory>, postcopy_steps: list[~django_copyist.config.PostcopyStep] = <factory>, static_filters: ~django.db.models.query_utils.Q | None = None)#

This class is a configuration that describes how a specific model should be copied.

Parameters:
  • model (Type[Model]) – The root model class that should be copied.

  • field_copy_actions (Dict[str, FieldCopyConfig]) – A dictionary storing information on copying each field. The key is the field name, and the value is a FieldCopyConfig instance. If a field is not present in field_copy_actions, it will be ignored and the default for the model will be used.

  • ignore_condition (IgnoreCondition, optional) – An optional instance of IgnoreCondition. This describes the condition on which copying of the model should be ignored, defaults to None.

  • compound_copy_actions (List[ModelCopyConfig], optional) – A list of ModelCopyConfig instances. The copying for these instances should be executed after copying the current model. This should be used if describing other models in a nested style in field_copy_actions is not appropriate (e.g., when copying another model depends on multiple fields), defaults to None.

  • filter_field_to_input_key (Dict[str, str], optional) – A dictionary mapping of model field names, which are used for the initial querying of the model, to input data key (e.g., {“id”: “player_id”} if you want to query by Player.id and you have the field “player_id” in input_data), defaults to None.

  • data_preparation_steps (List[DataPreparationStep], optional) – A list of DataPreparationStep instances. These can be used if special actions are needed to prepare the database state before copying, for example, if you need to delete some data in the target “location” before copying data from the origin, defaults to None.

  • postcopy_steps (List[PostcopyStep], optional) – A list of PostcopyStep instances. These can be used if special actions are needed after the model data is copied, defaults to None.

  • static_filters (Q, optional) – An optional Q instance. If present, this will be used as an additional filter for querying the given model, defaults to None.

protocol django_copyist.config.PostcopyFunc#

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

__annotations_cache__ = {}#
__call__(*, model_config: ModelCopyConfig, input_data: dict[str, Any], set_to_filter_map: SetToFilterMap, output_map: OutputMap, copy_intent_list: list[CopyIntent]) → None#

A protocol that defines a function to be used for post-copy operations.

This function is expected to take several parameters including the model configuration, input data, set to filter map, output map, and a list of copy intents. It does not return any value.

Parameters:
  • model_config (ModelCopyConfig) – The configuration for the model being copied.

  • input_data (Dict[str, Any]) – The input data for the copy request.

  • set_to_filter_map (SetToFilterMap) – The current global set to filter map.

  • output_map (OutputMap) – The current global output map.

  • copy_intent_list (List[CopyIntent]) – The list of copy intents. Copy intent stores information about original and copied model

class django_copyist.config.PostcopyStep(action: DataModificationActions, filter_field_to_input_key: dict[str, str] | None = None, func: PostcopyFunc | None = None)#

This class represents a step to be executed after the copying of model data.

Parameters:
  • action (Action) – The action to be executed.

  • filter_field_to_input_key (dict, optional) – An optional mapping of model field names to input data keys. This is used for the DELETE_BY_FILTER action. If present, it will be used as an additional filter for querying the given model, defaults to None.

  • func (PostcopyFunc, optional) – An optional instance of PostcopyFunc. If present, it will be executed after the copying of model data, defaults to None.

protocol django_copyist.config.SetToFilterFunc#

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

__annotations_cache__ = {}#
__call__(*, model_config: ModelCopyConfig, input_data: dict[str, Any], field_name: str, field_copy_config: FieldCopyConfig, set_to_filter_map: SetToFilterMap, instance_list: list[Model], referenced_instance_list: list[Model]) → FieldSetToFilterMap#

A protocol that defines a function to be used for setting the filter map for a field.

This function is expected to take several parameters including the model configuration, input data, field name, field copy configuration, set to filter map, instance list, and referenced instance list. It should return a FieldSetToFilterMap, which stores mapping of original object id string to substitute id or None.

Parameters:
  • model_config (ModelCopyConfig) – The configuration for the model being copied.

  • input_data (Dict[str, Any]) – The input data for the copy request.

  • field_name (str) – The name of the field for which the filter map is being set.

  • field_copy_config (FieldCopyConfig) – The copy configuration for the SET_TO_FILTER field.

  • set_to_filter_map (SetToFilterMap) – The current global set to filter map.

  • instance_list (List[Model]) – The list of instances of the model being copied.

  • referenced_instance_list (List[Model]) – The list of instances that are referenced by the field.

Returns:

The updated set to filter map for the field.

Return type:

FieldSetToFilterMap

django_copyist.config.TAKE_FROM_ORIGIN = FieldCopyConfig(action=<CopyActions.TAKE_FROM_ORIGIN: 'TAKE_FROM_ORIGIN'>, copy_with_config=None, reference_to=None, filter_config=None, input_key=None)#

Shortcut for creating FieldCopyConfig with TAKE_FROM_ORIGIN action

django_copyist.config.TakeFromInput(input_key: str) → FieldCopyConfig#

Shortcut for creating a FieldCopyConfig instance with the TAKE_FROM_INPUT action.

Parameters:

input_key (str) – The key in input_data that holds the value for the field.

Returns:

A FieldCopyConfig instance with the action set to TAKE_FROM_INPUT.

Return type:

FieldCopyConfig

django_copyist.config.UpdateToCopied(reference: type[Model]) → FieldCopyConfig#

This function is a shortcut for creating a FieldCopyConfig instance with the UPDATE_TO_COPIED action.

Parameters:

reference (Type[Model]) – The type of the model to which the field should be updated.

Returns:

A FieldCopyConfig instance with the action set to UPDATE_TO_COPIED and the reference set to the provided model type.

Return type:

FieldCopyConfig

django_copyist.config.map_key(model: type[Model], map_keys: MapKeys = MapKeys.MODEL_NAME) → str#

Key of model in the result maps for the given MapKeys mode.

Checking configs#

django_copyist.checks.check_config(config: ModelCopyConfig | CopyistConfig, *, raise_on_error: bool = False) → list[ConfigIssue]#

Check a copy configuration against the Django model metadata.

The check runs no queries. It finds problems that would otherwise surface in the middle of a copy, or not at all:

  • copyist.E001: unknown field name.

  • copyist.E002: action does not fit the field kind (for example MAKE_COPY on a forward foreign key).

  • copyist.E003: reference_to or a MakeCopy model does not match the relation.

  • copyist.E004: required field without default is not copied.

  • copyist.E005: unique field is copied from the origin.

  • copyist.E006: custom many-to-many through model has required extra columns.

  • copyist.E007: multi-table inheritance.

  • copyist.E008: root config without filter_field_to_input_key.

  • copyist.E009: model configured more than once.

  • copyist.E010: UpdateToCopied references a model that the config never copies.

  • copyist.E011: UpdateToCopied on a foreign key to the same model (a tree). Many-to-many links to the same model work.

  • copyist.E012: two models share a class name while maps are keyed by class name. Use CopyistConfig(..., map_keys=MapKeys.LABEL).

  • copyist.W001: field is not copied and gets its default.

  • copyist.W002: all fields of a unique set are copied from the origin.

  • copyist.W003: many-to-many field is not copied.

  • copyist.W004: custom through model has extra columns that get defaults.

  • copyist.W005: a foreign key to the same model is copied from the origin.

Parameters:
Returns:

All issues, errors first.

class django_copyist.checks.ConfigIssue(level: ConfigIssueLevel, code: str, model: str, field: str | None, message: str)#

One problem that check_config() found.

Variables:
  • level – ERROR or WARNING.

  • code – Stable identifier, for example copyist.E004. Use it to filter issues.

  • model – Label of the model whose config has the problem, for example demo.Task.

  • field – Field name, or None if the issue is about the whole model config.

  • message – What is wrong and how to fix it.

class django_copyist.checks.ConfigIssueLevel(*values)#
ERROR = 'ERROR'#

The copy fails or writes wrong data.

WARNING = 'WARNING'#

The copy runs, but probably not the way you expect.

Generating configs#

django_copyist.scaffold.scaffold_config(model: type[Model], *, depth: int = 1, name: str | None = None, input_key: str | None = None) → str#

Return Python source for a ModelCopyConfig of model.

The result is a starting point. It copies:

  • every concrete field, except the primary key, auto-filled fields and generated fields, with TAKE_FROM_ORIGIN;

  • unique fields with TakeFromInput, because the origin value would violate the constraint;

  • forward many-to-many fields with TAKE_FROM_ORIGIN;

  • one-to-many children with MakeCopy, down to depth levels.

Read the TODO comments, then run check_config() on the result.

Parameters:
  • model – Root model class.

  • depth – How many levels of one-to-many children to copy. 0 copies the root only.

  • name – Name of the generated constant. Defaults to <MODEL>_COPY_CONFIG.

  • input_key – input_data key that selects the root row. Defaults to <model>_pk.

Returns:

Source code of a Python module.