Question: 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?

Answer: 
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"

```
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')

```