Package: feincms3-data
WhyUtilities for loading and dumping database data as JSON.These utilities (partially) replace Django’s built-indumpdataandloaddatamanagement 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’sdumpdataandloaddatamanagement 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.Howpip installfeincms3-data.Addfeincms3_datatoINSTALLED_APPSso that the included management
commands are discovered.Add datasets somewhere describing the models and relationships you want to
dump, e.g. in a module namedapp.f3datasets:fromfeincms3_data.dataimport(specs_for_app_models,specs_for_derived_models,specs_for_models,)fromapp.dashboardimportmodelsasdashboard_modelsfromapp.worldimportmodelsasworld_modelsdefdistricts(args):pks=[int(arg)forarginargs.split(",")ifarg]return[*specs_for_models([world_models.District],{"filter":{"pk__in":pks},"delete_missing":True,},),*specs_for_models([world_models.Exercise],{"filter":{"district__in":pks},"delete_missing":True,},),# All derived non-abstract models which aren't proxies:*specs_for_derived_models(world_models.ExercisePlugin,{"filter":{"parent__district__in":pks},"delete_missing":True,},),]defdatasets():return{"articles":{"specs":lambdaargs:specs_for_app_models("articles",{"delete_missing":True},),},"pages":{"specs":lambdaargs:specs_for_app_models("pages",{"delete_missing":True},),},"teachingmaterials":{"specs":lambdaargs:specs_for_models([dashboard_models.TeachingMaterialGroup,dashboard_models.TeachingMaterial,],{"delete_missing":True},),},"districts":{"specs":districts,},}Add a setting with the Python module path to the specs function:FEINCMS3_DATA_DATASETS="app.f3datasets.datasets"Now, to dump e.g. pages you would run:./manage.py f3dumpdata pages > tmp/pages.jsonTo dump the districts with the primary key of 42 and 43 you would run:./manage.py f3dumpdata districts:42,43 > tmp/districts.jsonThe 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’sdumpdata, 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."delete_missing": This flag makes the loader delete all objects matching"filter"which do not exist in the dump."ignore_missing_m2m": A list of field names where deletions of related
models should be ignored when restoring. This may be especially useful when
only transferring content partially between databases."save_as_new": If present and truish, objects are inserted using new
primary keys into the database instead of (potentially) overwriting
pre-existing objects."defer_values": A list of fields which should receive random garbage when
loading initially and only receive their real value later. This is especially
useful to avoid unique constraint errors when loading partial graphs.The dumps can be loaded back into the database by running:./manage.py f3loaddata -v2 tmp/pages.json tmp/districts.jsonEach dump is processed in an individual transaction. The data is first loaded
into the database; at the end, datamatchingthe filters but whose primary
key wasn’t contained in the dump is deleted from the database (if"delete_missing": True).