Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django dump data for a single model?

As of version 1.1 and greater, the Django dumpdata management command allows you to dump data from individual tables:

./manage.py dumpdata myapp1 myapp2.my_model

You can also separate multiple apps and models on the command line. Here's the canonical definition:

django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]

As noted, you can't do this through a manage.py command in Django 1.0. However you could use a script to export the JSON file, and load it using loaddata:

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()

Take all data into json format from django model.

Syntax:

python manage.py dumpdata app_name.model_name

For example dumping data from group_permission model which reside in default auth app in django.

python manage.py dumpdata auth.group_permission

For output take a look on console.


A specific model to a specific file:

python manage.py dumpdata app_label.ModelName > specific_file.json

and to load it to another app: first move or copy the file to the app where you want process it, then:

python manage.py loaddata specific_file.json

I think you had the solution in your question. You can dump an individual model like this:

./manage.py dumpdata myapp.my_model

For success I had to say it twice, and specify the model two times, like:

./manage.py dumpdata myapp2.my_model myapp2.my_model

If I only said

./manage.py dumpdata myapp2 myapp2.my_model

I got flooded with all the models in myapp2, despite the fact that I specified my_model.