[Django]-How to dump part of my database in Django

4👍

You can change this part models.MyModel.objects.all() to get selective data in fixtures.

from django.core import serializers
from myproject.myapp import models
data = serializers.serialize("json", models.MyModel.objects.all())
out = open("mymodel.json", "w")
out.write(data)
out.close()
👤Ahsan

1👍

Need to use dumpdata e.g.

python manage.py dumpdata --format=json --indent=2 --exclude=admin --exclude=sessions > test_db.json

Here I am dumping everything in the database excluding the admin and sessions tables (my guess is you might not need those), into a json file named test_db.json. I’m also using an indent of 2 spaces to make the file more easy to inspect by eye.

👤diliop

0👍

You can try to use mysqldump :

You will need to lookup all the names of the tables you want to dump. To get the full list you can use :

mysqlshow db_name

Then run :

mysqldump db_name table_1 table_2 table_3 table_4

This command will output the result to the standard output, if you want it to write to a file, use --result-file=/path/to/file

See also the full documentation for mysqldump : http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html

Leave a comment