How to make db dumpfile in django
Asked Answered
D

5

28

I want to make a dump in django irrespective of database I am using and can be loaded later. The command 'dumpdata' is perfect for this, but it is printing output on console. More over I am calling it using call_command function so I cannot store its content in any variable as it is printing output on console.

Please let me know how store dump to a file using dumpdata or any other command or api.

Thanks

Dasha answered 18/7, 2012 at 16:34 Comment(1)
I misread your question initially, but I've added an answer that uses call_command.Chetchetah
C
32

You can choose a file to put the output of dumpdata into if you call it from within Python using call_command, for example:

from django.core.management import call_command

output = open(output_filename,'w') # Point stdout at a file for dumping data to.
call_command('dumpdata','model_name',format='json',indent=3,stdout=output)
output.close()

However, if you try calling this from the command line with e.g. --stdout=filename.json at the end of your dumpdata command, it gives the error manage.py: error: no such option: --stdout.

So it is there, you just have to call it within a Python script rather than on the command line. If you want it as a command line option, then redirection (as others have suggested) is your best bet.

Chetchetah answered 18/7, 2012 at 17:20 Comment(1)
Great answer! But indent=3 ?? You're breaking my 4-space-tabbed python heart.Pigeonhole
R
44

You just use it like that:

./manage.py dumpdata > data_dump.json

After that action, there will be data_dump.json file in the directory in which you executed that command.

There are multiple options coming with that, but you probably already know it. The thing you need to know is how to redirect output from standard output into some file: you perform that action by putting > before file name.

To append something to the file you would use >>, but since you are dumping the data from Django and the output is most likely JSON, you will not want that (because it will make JSON invalid).

Remindful answered 18/7, 2012 at 16:39 Comment(7)
can u tell me how to do it without using redirection, remember is platform dependentDasha
@ParitoshSingh Redirecting with > works in Unix (and thus OS X) and in Windows; are there other platforms you'd be running this on?Chetchetah
@Chetchetah ofcourse i wish to make it platform independent, i.e. why i am using django dump data, if that had been the case i would have written making dumpfile specific to db i am using....Dasha
@ParitoshSingh: If you would like to have full platform independence, you should probably write your own solution and address all the platforms. If the targeted platform is able to run manage.py script's dumpdata command, then you just do this by catching the output of this command and putting it into a file. In case of Unix (Linux, OS X) and Windows (as noted by bouteillebleu and seen here) the shortcut for "catching the output and putting it into a file" is > output redirection.Remindful
@ParitoshSingh: bouteillebleu correctly noted it is platform independent, unless you are trying to write it for very uncommon platform (but I would rather doubt whether this platform is even capable of running Django correctly). Could you name the platform on which the mentioned solution would not be able to work?Remindful
@ParitoshSingh I've added an answer about how to redirect the output from within Python, but when I checked it didn't work as a command line option; that might help you if you're looking to do it from within a script, at least.Chetchetah
@Chetchetah ya correct, but i was considering a case where these redirections are not working. I am not aware about other platforms like Mac, so i kept this conditiion and it will be better if we make it generic process and also as i asked in question i am running dummpdata using call_command function, how can i use redirection there, there should be some other way out.Dasha
C
32

You can choose a file to put the output of dumpdata into if you call it from within Python using call_command, for example:

from django.core.management import call_command

output = open(output_filename,'w') # Point stdout at a file for dumping data to.
call_command('dumpdata','model_name',format='json',indent=3,stdout=output)
output.close()

However, if you try calling this from the command line with e.g. --stdout=filename.json at the end of your dumpdata command, it gives the error manage.py: error: no such option: --stdout.

So it is there, you just have to call it within a Python script rather than on the command line. If you want it as a command line option, then redirection (as others have suggested) is your best bet.

Chetchetah answered 18/7, 2012 at 17:20 Comment(1)
Great answer! But indent=3 ?? You're breaking my 4-space-tabbed python heart.Pigeonhole
L
14

As it is mention in the docs, in order to dump large datasets you can avoid the sections causing problems, and treat them separately.

The following command does generally work:

python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

python manage.py loaddata db.json

In case you can export later the excluded data:

python manage.py dumpdata auth.permission > auth.json

python manage.py loaddata auth.json
Lyon answered 13/9, 2016 at 22:12 Comment(1)
this is really a life saver as I had fixture serialization issuesIngeringersoll
C
4

django-admin.py dumpdata

Outputs to standard output all data in the database associated with the named application(s).

As you know, you can redirect standard output to a file:

command > file.data
Crawley answered 18/7, 2012 at 16:39 Comment(1)
This is the best answer in my opinion because it answers his core problem: Please let me know how store dump to a file using dumpdata or any other command or api. Using the linux built-in seems completely appropriate.Dugong
C
2

You can dump all database models data by using below code

from django.core.management import call_command


with open("data.json", "w", encoding="utf-8") as fp:
    call_command("dumpdata", format="json", indent=2, stdout=fp)

This will create a file (data.json) in the root of your project folder.

Console answered 25/7, 2021 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.