| 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, and 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.