views:

2065

answers:

5

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

E.g. for an app it would be...

python manage.py dumpdata myapp

However, I want some specific model, (e.g. myapp.mymodel) to be dumped... reason being I have some huge datasets (3 million records plus) in the same app that I would not like dumped.

+1  A: 

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

./manage.py dumpdata myapp.my_model

Harold
i tried it before posting. had no luck. i'm in django 1.0. also tried myapp.models.mymodel?
nategood
you need to be using trunk for this to work.
nbv4
+10  A: 

Well, this was indeed a required functionality. And it was added, too. So you can dump individual models using dumpdata. But it was added after the v1.1 alpha 1 release.

Syntax is same.

./manage.py dumpdata myapp1 myapp2.my_model

You'll need to be using a recent trunk checkout to access this feature. But as you said you are using v1.0. Hard luck.

simplyharsh
+1  A: 

As a workaround you could make another app and copy the model but point it to the existing table with the db_table meta option. Then you could just dump the models you copied into the new app. You existing app wouldn't be affected.

Dennis Baker
+10  A: 

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()
dar
+1  A: 

Great tip! And it works well.

One more tip: The indent=4 makes it more readable:

data = serializers.serialize("json", models.MyModel.objects.all(),indent=4)