Django dump data for a single model?

DjangoDjango ModelsLoaddataDumpdata

Django Problem Overview


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.

Django Solutions


Solution 1 - Django

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 - Django

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 - Django

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

Solution 4 - Django

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.

Solution 5 - Django

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

./manage.py dumpdata myapp.my_model

Solution 6 - Django

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.

Solution 7 - Django

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.

Solution 8 - Django

I've created a management command the generate a fixture on a per model basis. Fixtures can be generated by running:

./manage generate_fixtures app.model.MyModel --file=dump/MyModel.json

code at: https://gist.github.com/2394883

Solution 9 - Django

To write it on specific file:

python manage.py dumpdata app_label.ModelName app_label.ModelName2 > fixtures/specic.json

Solution 10 - Django

If you want to dump only the specified objects of a model, you can use the --pks optional argument for the dumpdata command.

> --pks PRIMARY_KEYS Outputs only the objects specified by a comma separated list of primary keys. This is only available when dumping > one model. By default, all the records of the model are output.

Solution 11 - Django

For newbies in Django and Python like me, this might be useful:

if you want to only dump a single row (of, obviously, a single table) and you have for example the app "merchant" and the model is also named "Merchant", and you usually import it using a fully qualified name like this: merchant.models.Merchant; do not even try to use this name. The syntax is as follows:

# only dumps the merchant with id = 123, pretty printed
python manage.py dumpdata merchant.Merchant --pks 123 --indent 2

Solution 12 - Django

For DUMPING data out of the specific model for specific APP.


If we take EXAMPLE where we have in a MAIN PROJECT (We go call it project) and in this MAIN PROJECT we have two (02) others applications (We go call each app like this APP1 and APP2) we can do like that


  1. python manage.py dumpdata APP1.name_of_model >APP1/fixtures/name_of_model .json
  2. python manage.py dumpdata APP2.name_of_model >APP2/fixtures/name_of_model .json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnategoodView Question on Stackoverflow
Solution 1 - DjangosimplyharshView Answer on Stackoverflow
Solution 2 - DjangodarView Answer on Stackoverflow
Solution 3 - Djangosipi09View Answer on Stackoverflow
Solution 4 - DjangoShubho ShahaView Answer on Stackoverflow
Solution 5 - DjangoHaroldView Answer on Stackoverflow
Solution 6 - DjangoCsaba TothView Answer on Stackoverflow
Solution 7 - DjangoOgre CodesView Answer on Stackoverflow
Solution 8 - DjangoJonas GeiregatView Answer on Stackoverflow
Solution 9 - DjangoNikkoView Answer on Stackoverflow
Solution 10 - DjangodtatarkinView Answer on Stackoverflow
Solution 11 - DjangoClint EastwoodView Answer on Stackoverflow
Solution 12 - DjangoIsrael KuassiView Answer on Stackoverflow