Reputation: 962
I want to use exist data in my database for unit test in Django. The database is too big for a test so I want to dump part of the data.
Is there any way to dump part of them? (my database is MySQL)
Upvotes: 3
Views: 2703
Reputation: 6213
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
Upvotes: 0
Reputation: 9451
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.
Upvotes: 1
Reputation: 11832
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()
Upvotes: 4