How to use call_command with dumpdata command to save json to file
Asked Answered
S

5

23

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?

Snicker answered 18/4, 2013 at 6:16 Comment(0)
S
28

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
Snicker answered 18/4, 2013 at 6:36 Comment(1)
Thanks Amyth. This is what I had to use when using django-fixture-magic to do a custom_dump in an admin action.Stepaniestepbrother
U
20

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())
Unsuccessful answered 9/12, 2013 at 20:40 Comment(5)
I think create_fixture can be simplified: with open(filename, 'w') as f: management.call_command('dumpdata', app_name, stdout=f)Speight
agreed with @AurélienGâteau, but if you do intend on intercepting with StringIO as of Python 3 make sure to swap cStringIO for ioSolothurn
also management.call_command('dumpdata', app_name, '-o', filename)Unveiling
@AurélienGâteau Can you make that a separate answer then?Cushing
@Cushing nothing like re-reading an answer from 9 years ago to realize mistakes :). The way I understand it, the original answer ID was to provide a way to modify the stream before writing it. Probably between the call to buf.seek(0) and with open(...), in this case my suggestion does not make sense.Speight
S
3

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"
Stepaniestepbrother answered 30/7, 2014 at 17:15 Comment(0)
W
3

DB fixtures typically compress well, and loaddata can read compressed fixtures. To write a .bz2 compressed fixture directly:

import bz2

with bz2.BZ2File('db.json.bz2', 'w', buffering=1024) as f:
  django.core.management.call_command('dumpdata', stdout=f)
Walley answered 19/6, 2017 at 14:55 Comment(0)
A
0

This one help for multiple dump data into json file

from django.core.management import call_command
import sys


sys.stdout = open('app_one/fixtures/apple.json', 'w')
call_command('dumpdata', 'app_one.apple')

sys.stdout = open('app_two/fixtures/banana.json', 'w')
call_command('dumpdata', 'app_two.banana')
Allowedly answered 22/4, 2020 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.