File: dumpscript.rst

package info (click to toggle)
python-django-extensions 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,396 kB
  • sloc: python: 11,858; makefile: 116
file content (87 lines) | stat: -rw-r--r-- 2,524 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
dumpscript
==========

: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.

Why?
----

There are a few benefits to this:

* less drama with model evolution: foreign keys handled naturally without IDs,
  new and removed columns are ignored
* edit script to create 1,000s of generated entries using for loops, generated
  names, python modules etc.

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.


Features
--------

* *ForeignKey* and *ManyToManyFields* (using python variables, not object IDs)
* Self-referencing *ForeignKey* (and M2M) fields
* Sub-classed models
* *ContentType* fields and generic relationships
* Recursive references
* *AutoFields* are excluded
* Parent models are only included when no other child model links to it
* Individual models can be referenced


How?
----

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.


Caveats
-------

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