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?
You need the dumpdata
management command.
pip install pyyaml
python manage.py dumpdata --format=yaml > /path/to/dump_file.yaml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With