Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use call_command with dumpdata command to save json to file

I am trying to use the call_command method to call the dumpdata command. Manually, I use it as follows to save the data to a file.

python manage.py dumpdata appname_one appname_two > /path/to/save/file.json

and it saves the json file. Now, I am in a situation where I need to call this command using the call_command method.

I am able to print out the json from the command using the following:

from django.core.management import call_command

call_command('dumpdata', 'appname_one', 'appname_two')

Is there a way I can save the given data to a file like we do it from the command line?

like image 653
Amyth Avatar asked Apr 18 '13 06:04

Amyth


People also ask

How do I run manage py in Runserver?

1) python manage.py runserver So, after running it, you can go to localhost:8000 or 127.0. 0.1:8000. Requirements: You must run this in the ROOT of your django project where manage.py lives. Useful subcommand: python manage.py runserver <yourport> so you can change the port from above.

What is django-admin commands?

django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.

What is manage py in Python?

Manage.py in Django is a command-line utility that works similar to the django-admin command. The difference is that it points towards the project's settings.py file. This manage.py utility provides various commands that you must have while working with Django.

What does the Django command manage py shell do?

When you run python manage.py shell you run a python (or IPython) interpreter but inside it load all your Django project configurations so you can execute commands against the database or any other resources that are available in your Django project.


3 Answers

had to redirect sys.stdout to the file in order to achieve the above. Something like.

import sys

from django.core.management import call_command


sysout = sys.stdout
sys.stdout = open('filename.json', 'w')
call_command('dumpdata', 'appname_one', 'appname_two')
sys.stdout = sysout
like image 113
Amyth Avatar answered Oct 22 '22 11:10

Amyth


An even better way is to use Django's built-in stdout redirection for their command modules. See docs here.

If you want to manipulate the stream before sending it to a file, you can also pass it a StringIO buffer:

import os
from cStringIO import StringIO

from django.core import management

def create_fixture(app_name, filename):
    buf = StringIO()
    management.call_command('dumpdata', app_name, stdout=buf)
    buf.seek(0)
    with open(filename, 'w') as f:
        f.write(buf.read())
like image 38
mgarabed Avatar answered Oct 22 '22 11:10

mgarabed


I am using Django fixture magic https://github.com/davedash/django-fixture-magic and need to dump a custom fixture. I tried several ways but ended up using Amyth's answer becuase it was the only way that worked.

Here is my admin action that works with fixture magic

def export_survey(modeladmin, request, queryset):

    sysout = sys.stdout

    survey = queryset[0]
    fname = "%s.json" %(survey.slug)
    response = HttpResponse(mimetype='application/json')
    response['Content-Disposition'] = 'attachment; filename=%s' %(fname)

    sys.stdout = response
    call_command('custom_dump', 'complete_survey', survey.id)
    sys.stdout = sysout
    return response

export_survey.short_description = "Exports a single survey as a .json file"
like image 3
wilblack Avatar answered Oct 22 '22 12:10

wilblack