Django dump data for a single model?

Can I perform a dumpdata in Django on just a single model, rather than the whole app, and if so, how?

For an app it would be:

python manage.py dumpdata myapp

However, I want some specific model, such as "myapp.mymodel" to be dumped. The reason being, I have some huge, 3 million records plus, datasets in the same app that I would not like dumped.


Solution 1:

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] ...]]

Solution 2:

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

Solution 3:

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.