Paritosh Singh Paritosh Singh - 1 year ago 83
Python Question

How to make db dumpfile in django

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.


Answer Source

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 import call_command

output = open(output_filename,'w') # Point stdout at a file for dumping data to.

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 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download