Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Django Database into YAML file

I know you can easily import a YAML file into a Django database (in order to populate the database before starting the project for instance) but how can I do the opposite (ie save the complete database into a single .yaml file).

I read there is a way to export one single table into a file:

YAMLSerializer = serializers.get_serializer("yaml")
yaml_serializer = YAMLSerializer()
with open("file.yaml", "w") as out:
    yaml_serializer.serialize(SomeModel.objects.all(), stream=out)

but I need to do it on the complete database (which has many tables with complex relations between each ones).

I could write a script to do that for me, but I don't want to redo something which has probably been done already, and I wouldn't know how to do it the better way so that Django has no difficulties to read it after.

So far, I've been working on a SQLITE3 database engine.

Any ideas?

like image 429
Julien Greard Avatar asked Jan 16 '14 17:01

Julien Greard


1 Answers

You need the dumpdata management command.

pip install pyyaml
python manage.py dumpdata --format=yaml > /path/to/dump_file.yaml
like image 166
elssar Avatar answered Sep 19 '22 00:09

elssar