Skip to main content

No project description provided

Project description

CI Status

Why

Utilities for loading and dumping database data as JSON.

These utilities (partially) replace Django’s built-in dumpdata and loaddata management commands.

Suppose you want to move data between systems incrementally. In this case it isn’t sufficient to only know the data which has been created or updated; you also want to know which data has been deleted in the meantime. Django’s dumpdata and loaddata management commands only support the former case, not the latter. They also do not including dependent objects in the dump.

This package offers utilities and management commands to address these shortcomings.

How

pip install feincms3-data.

Add feincms3_data to INSTALLED_APPS so that the included management commands are discovered.

Add specs somewhere describing the models and relationships you want to dump, e.g. in a module named app.specs:

from feincms3_data.data import (
    specs_for_app_models,
    specs_for_derived_models,
    specs_for_models,
)
from app.dashboard import models as dashboard_models
from app.world import models as world_models


def districts(args):
    pks = [int(arg) for arg in args.split(",") if arg]
    return [
        *specs_for_models(
            [world_models.District],
            {"filter": {"pk__in": pks}},
        ),
        *specs_for_models(
            [world_models.Exercise],
            {"filter": {"district__in": pks}},
        ),
        *specs_for_derived_models(
            world_models.ExercisePlugin,
            {"filter": {"parent__district__in": pks}},
        ),
    ]


def specs():
    return {
        "articles": lambda args: specs_for_app_models("articles"),
        "pages": lambda args: specs_for_app_models("pages"),
        "teachingmaterials": lambda args: specs_for_models(
            [
                dashboard_models.TeachingMaterialGroup,
                dashboard_models.TeachingMaterial,
            ]
        ),
        "districts": districts,
    }

Add a setting with the Python module path to the specs function:

FEINCMS3_DATA_SPECS = "app.specs.specs"

Now, to dump e.g. pages and teachingmaterials you would run:

./manage.py f3dumpdata pages teachingmaterials > tmp/dump.json

To dump the districts with the primary key of 42 and 43 you would run:

./manage.py f3dumpdata districts:42,43 > tmp/districts.json

The resulting JSON file has three top-level keys:

  • "version": 1: The version of the dump, because not versioning dumps is a recipe for pain down the road.

  • "specs": [...]: A list of model specs.

  • "objects": [...]: A list of model instances; uses the same serializer as Django’s dumpdata, everything looks the same.

Model specs consist of the following fields:

  • "model": The lowercased label (app_label.model_name) of a model.

  • "filter": A dictionary which can be passed to the .filter() queryset method as keyword arguments; used for determining the objects to dump and the objects to remove after loading.

  • "save_as_new": If present and truish, objects are inserted using new primary keys into the database instead of (potentially) overwriting pre-existing objects.

  • "delete_missing": This flag makes the loader delete all objects matching "filter" which do not exist in the dump.

The dumps can be loaded back into the database by running:

./manage.py f3loaddata -v2 tmp/dump.json tmp/districts.json

Each dump is processed in an individual transaction. The data is first loaded into the database; at the end, data matching the filters but whose primary key wasn’t contained in the dump is deleted from the database (if "delete_missing": True).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

feincms3_data-0.0.6.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

feincms3_data-0.0.6-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file feincms3_data-0.0.6.tar.gz.

File metadata

  • Download URL: feincms3_data-0.0.6.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.10

File hashes

Hashes for feincms3_data-0.0.6.tar.gz
Algorithm Hash digest
SHA256 9c8e4ccd2ab0b1323d4c84dbd96d32fa6234a399a9b9a6fdfdfb03d32e7b64dc
MD5 58eab68946e4777640936176c0865458
BLAKE2b-256 14d961d903495440b8e750ffaaa167b269b595fd33285da5e2743c137bcfbafc

See more details on using hashes here.

File details

Details for the file feincms3_data-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: feincms3_data-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.10

File hashes

Hashes for feincms3_data-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e4891f6eb9da4073c6a6d9c1baff84bdea3fb45baf3aaf5c1169be649e14892b
MD5 0221ef47872eca274bf7adc671b120a4
BLAKE2b-256 357e9138f133a2839b1ccea08ab8540df2ce7c5fc003e317725a2c36a4b981dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page