如何使用call_command和dumpdata命令将json保存到文件中

时间:2021-04-24 13:54:14

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.

我正在尝试使用call_command方法调用dumpdata命令。手动地,我使用它来将数据保存到文件中。

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.

它保存了json文件。现在,我需要使用call_command方法调用这个命令。

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

我可以使用以下命令从命令中打印出json:

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?

是否有一种方法可以像在命令行中那样将给定的数据保存到文件中?

4 个解决方案

#1


11  

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

不得不重定向系统。为了达到上述目的,对文件进行stdout。喜欢的东西。

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

#2


10  

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

更好的方法是将Django的内置stdout重定向用于它们的命令模块。看到文档。

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

如果您希望在将流发送到文件之前对其进行操作,您还可以将它传递给一个StringIO缓冲区:

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())

#3


2  

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.

我正在使用Django fixture magic https://github.com/davedash/django- magic,并需要转储一个定制的fixture。我试了好几种方法,但最后还是用了阿米斯的答案,因为这是唯一可行的方法。

Here is my admin action that works with fixture magic

下面是我的管理动作,它使用了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"

#4


0  

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

DB fixture很好地压缩,loaddata可以读取压缩的fixture。直接编写.bz2压缩夹具:

import bz2

with bz2.BZ2File('db.json.bz2', 'w', buffering=1024) as f:
  django.core.management.call_command('dumpdata', stdout=f)

#1


11  

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

不得不重定向系统。为了达到上述目的,对文件进行stdout。喜欢的东西。

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

#2


10  

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

更好的方法是将Django的内置stdout重定向用于它们的命令模块。看到文档。

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

如果您希望在将流发送到文件之前对其进行操作,您还可以将它传递给一个StringIO缓冲区:

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())

#3


2  

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.

我正在使用Django fixture magic https://github.com/davedash/django- magic,并需要转储一个定制的fixture。我试了好几种方法,但最后还是用了阿米斯的答案,因为这是唯一可行的方法。

Here is my admin action that works with fixture magic

下面是我的管理动作,它使用了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"

#4


0  

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

DB fixture很好地压缩,loaddata可以读取压缩的fixture。直接编写.bz2压缩夹具:

import bz2

with bz2.BZ2File('db.json.bz2', 'w', buffering=1024) as f:
  django.core.management.call_command('dumpdata', stdout=f)