django-dex

Database export tools for Django


Keywords
django, database_export
License
MIT
Install
pip install django-dex==0.3.3

Documentation

Django Dex

Database export tools:

  • Clone a Django database into another Django database
  • Export database records to files

Install

Install:

pip install django-dex

Add to installed apps:

"introspection",
"dex",

Management commands

Export a database to another database

python3 manage.py clonedb default replica -a -m

Where default and replica are registered databases in settings.DATABASES:

Databases settings

DATABASES = {
   'default': {
      # ...
    },
    'replica': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME': os.path.join(BASE_DIR, 'replica.sqlite3'),
    }
}

Options:

-apps: apps to export:

python3 manage.py clonedb default replica -apps=auth,myapp1,myapp2

-a: archive the last replica

-m: to migrate the destination database before copying data

-verb: verbosity level. To mute it:

python3 manage.py clonedb default replica -verb 0

Export database records to files

Use a Django extensions shell or script: example:

from dex.export import Exporter
from myapp.models import MyModel

def process(row):
    content = row["col1"] + row["col2"]
    return content

q = MyModel.objects.all()
# parameters are: query, destination, slug field, content field, file extension
# and optionaly a function to process the records before saving
Exporter().tofiles(q, "tmp", "url", "content", "html", process)

Parameters for the tofiles method:

  • query: a Django orm query
  • destination: the destination folder where to save the files
  • slug_field: name of the field from where to take the file name
  • content_field: name of the field used to populate file content
  • extension: the file extension
  • 'func': optional: a function used to process the row before saving content

Commands for django-term

   pip install django-downloadview

Add "django_downloadview", to installed apps.

Add url('^dex/', include('dex.urls')), to urls.

These are command that run into Django Term

replicatedb: to replicate the default database to the replica sqlite database.

This command is used to clone the default db into a sqlite replica and download it.

Todo

  • Add an option to use bulk_create when there is no many-to-many relations
  • Humanize time duration
  • Export only one model
  • Model level database routing
  • Better looking output
  • Register models to auto export on save