30๐
You can choose a file to put the output of dumpdata into if you call it from within Python using call_command
, for example:
from django.core.management import call_command
output = open(output_filename,'w') # Point stdout at a file for dumping data to.
call_command('dumpdata','model_name',format='json',indent=3,stdout=output)
output.close()
However, if you try calling this from the command line with e.g. --stdout=filename.json
at the end of your dumpdata command, it gives the error manage.py: error: no such option: --stdout
.
So it is there, you just have to call it within a Python script rather than on the command line. If you want it as a command line option, then redirection (as others have suggested) is your best bet.
42๐
You just use it like that:
./manage.py dumpdata > data_dump.json
After that action, there will be data_dump.json
file in the directory in which you executed that command.
There are multiple options coming with that, but you probably already know it. The thing you need to know is how to redirect output from standard output into some file: you perform that action by putting >
before file name.
To append something to the file you would use >>
, but since you are dumping the data from Django and the output is most likely JSON, you will not want that (because it will make JSON invalid).
- [Django]-Users in initial data fixture
- [Django]-Django persistent database connection
- [Django]-Foreign key from one app into another in Django
12๐
As it is mention in the docs, in order to dump large datasets you can avoid the sections causing problems, and treat them separately.
The following command does generally work:
python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
python manage.py loaddata db.json
In case you can export later the excluded data:
python manage.py dumpdata auth.permission > auth.json
python manage.py loaddata auth.json
- [Django]-How to create objects on the fly in python?
- [Django]-GeoDjango on Windows: "Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found"
- [Django]-Django REST Framework custom fields validation
4๐
Outputs to standard output all data in the database associated with
the named application(s).
As you know, you can redirect standard output to a file:
command > file.data
- [Django]-Check if a OneToOne relation exists in Django
- [Django]-Testing custom admin actions in django
- [Django]-Cancel saving model when using pre_save in django
2๐
You can dump all database models data by using below code
from django.core.management import call_command
with open("data.json", "w", encoding="utf-8") as fp:
call_command("dumpdata", format="json", indent=2, stdout=fp)
This will create a file (data.json
) in the root of your project folder.
- [Django]-Django ModelChoiceField optgroup tag
- [Django]-How to run tests in parallel in Django?
- [Django]-How can I register a single view (not a viewset) on my router?