How to make db dumpfile in django

Viewed 26833

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

5 Answers

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.

Related