Harry
Harry

Reputation: 13329

Django dumpdata from a python script

Im trying to do a dump data from a script like this:

from django.conf import settings
from django.core.management import call_command

settings.configure()
call_command('dumpdata','document_manager.%s' % model_name,format='json',indent=3,stdout=output)

This produces the error:

Unknown application: document_manager

The script is within another directory, its not an app, just a directory, I added an

__init__.py

file in this directory

Running the dumpdata from within the root of the app, it works. I guess its something todo with relative position of the script? I should I be doing this?

settings:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',

    'document_manager',
)

the dir structure looks like this: (django 1.5)

myappname
  /myappname
    settings.py
  /document_manager
  /cleaner
    __init__.py
    my_script.py

Upvotes: 2

Views: 2729

Answers (1)

crazyzubr
crazyzubr

Reputation: 1082

I advise you to use another method:

https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

To begin to create a structure:

document_manager/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            specdumpdata.py
    tests.py
    views.py

In specdumpdata.py:

from django.core.management.commands.dumpdata import Command as DumpDataCommand


class Command(DumpDataCommand):

    def handle(self, *app_labels, **options):
        # override options, app_labels and code method

        # with or without ...
        super(Command, self).handle(*app_labels, **options)

Run command in virtual environment:

python manage.py specdumpdata

Upvotes: 4

Related Questions