docs/dumpscript.rst
:synopsis: Generates a standalone Python script that will repopulate the database using objects.
The dumpscript command generates a standalone Python script that will
repopulate the database using objects. The advantage of this approach is that
it is easy to understand, and more flexible than directly populating the
database, or using XML.
There are a few benefits to this:
For example, an edited script can populate the database with test data::
for i in xrange(2000): poll = Poll() poll.question = "Question #%d" % i poll.pub_date = date(2001,01,01) + timedelta(days=i) poll.save()
Real databases will probably be bigger and more complicated so it is useful to enter some values using the admin interface and then edit the generated scripts.
To dump the data from all the models in a given Django app (appname)::
$ ./manage.py dumpscript appname > scripts/testdata.py
To dump the data from just a single model (appname.ModelName)::
$ ./manage.py dumpscript appname.ModelName > scripts/testdata.py
To reset a given app, and reload with the saved data::
$ ./manage.py reset appname $ ./manage.py runscript testdata
Note: Runscript needs scripts to be a module, so create the directory and a init.py file.
Naming conflicts
Please take care that when naming the output files these filenames do not
clash with other names in your import path. For instance, if the appname is
the same as the script name, an importerror can occur because rather than importing
the application modules it tries to load the modules from the dumpscript file itself.
Examples::
# Wrong
$ ./manage.py dumpscript appname > dumps/appname.py
# Right
$ ./manage.py dumpscript appname > dumps/appname_all.py
# Right
$ ./manage.py dumpscript appname.Somemodel > dumps/appname_somemodel.py