django-admin.txt 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. ==================================
  2. ``django-admin`` and ``manage.py``
  3. ==================================
  4. ``django-admin`` is Django's command-line utility for administrative tasks.
  5. This document outlines all it can do.
  6. In addition, ``manage.py`` is automatically created in each Django project. It
  7. does the same thing as ``django-admin`` but also sets the
  8. :envvar:`DJANGO_SETTINGS_MODULE` environment variable so that it points to your
  9. project's ``settings.py`` file.
  10. The ``django-admin`` script should be on your system path if you installed
  11. Django via its ``setup.py`` utility. If it's not on your path, you can find it
  12. in ``site-packages/django/bin`` within your Python installation. Consider
  13. symlinking it from some place on your path, such as ``/usr/local/bin``.
  14. For Windows users, who do not have symlinking functionality available, you can
  15. copy ``django-admin.exe`` to a location on your existing path or edit the
  16. ``PATH`` settings (under ``Settings - Control Panel - System - Advanced -
  17. Environment...``) to point to its installed location.
  18. Generally, when working on a single Django project, it's easier to use
  19. ``manage.py`` than ``django-admin``. If you need to switch between multiple
  20. Django settings files, use ``django-admin`` with
  21. :envvar:`DJANGO_SETTINGS_MODULE` or the :option:`--settings` command line
  22. option.
  23. The command-line examples throughout this document use ``django-admin`` to
  24. be consistent, but any example can use ``manage.py`` or ``python -m django``
  25. just as well.
  26. Usage
  27. =====
  28. .. console::
  29. $ django-admin <command> [options]
  30. $ manage.py <command> [options]
  31. $ python -m django <command> [options]
  32. ``command`` should be one of the commands listed in this document.
  33. ``options``, which is optional, should be zero or more of the options available
  34. for the given command.
  35. Getting runtime help
  36. --------------------
  37. .. django-admin:: help
  38. Run ``django-admin help`` to display usage information and a list of the
  39. commands provided by each application.
  40. Run ``django-admin help --commands`` to display a list of all available
  41. commands.
  42. Run ``django-admin help <command>`` to display a description of the given
  43. command and a list of its available options.
  44. App names
  45. ---------
  46. Many commands take a list of "app names." An "app name" is the basename of
  47. the package containing your models. For example, if your :setting:`INSTALLED_APPS`
  48. contains the string ``'mysite.blog'``, the app name is ``blog``.
  49. Determining the version
  50. -----------------------
  51. .. django-admin:: version
  52. Run ``django-admin version`` to display the current Django version.
  53. The output follows the schema described in :pep:`440`::
  54. 1.4.dev17026
  55. 1.4a1
  56. 1.4
  57. Displaying debug output
  58. -----------------------
  59. .. program:: None
  60. Use :option:`--verbosity` to specify the amount of notification and debug
  61. information that ``django-admin`` prints to the console.
  62. Available commands
  63. ==================
  64. ``check``
  65. ---------
  66. .. django-admin:: check [app_label [app_label ...]]
  67. Uses the :doc:`system check framework </ref/checks>` to inspect the entire
  68. Django project for common problems.
  69. By default, all apps will be checked. You can check a subset of apps by
  70. providing a list of app labels as arguments::
  71. django-admin check auth admin myapp
  72. If you do not specify any app, all apps will be checked.
  73. .. django-admin-option:: --tag TAGS, -t TAGS
  74. The system check framework performs many different types of checks that are
  75. :ref:`categorized with tags <system-check-builtin-tags>`. You can use these
  76. tags to restrict the checks performed to just those in a particular category.
  77. For example, to perform only models and compatibility checks, run::
  78. django-admin check --tag models --tag compatibility
  79. .. django-admin-option:: --list-tags
  80. Lists all available tags.
  81. .. django-admin-option:: --deploy
  82. Activates some additional checks that are only relevant in a deployment setting.
  83. You can use this option in your local development environment, but since your
  84. local development settings module may not have many of your production settings,
  85. you will probably want to point the ``check`` command at a different settings
  86. module, either by setting the ``DJANGO_SETTINGS_MODULE`` environment variable,
  87. or by passing the ``--settings`` option::
  88. django-admin check --deploy --settings=production_settings
  89. Or you could run it directly on a production or staging deployment to verify
  90. that the correct settings are in use (omitting ``--settings``). You could even
  91. make it part of your integration test suite.
  92. .. django-admin-option:: --fail-level {CRITICAL,ERROR,WARNING,INFO,DEBUG}
  93. Specifies the message level that will cause the command to exit with a non-zero
  94. status. Default is ``ERROR``.
  95. ``compilemessages``
  96. -------------------
  97. .. django-admin:: compilemessages
  98. Compiles ``.po`` files created by :djadmin:`makemessages` to ``.mo`` files for
  99. use with the built-in gettext support. See :doc:`/topics/i18n/index`.
  100. .. django-admin-option:: --locale LOCALE, -l LOCALE
  101. Specifies the locale(s) to process. If not provided, all locales are processed.
  102. .. django-admin-option:: --exclude EXCLUDE, -x EXCLUDE
  103. Specifies the locale(s) to exclude from processing. If not provided, no locales
  104. are excluded.
  105. .. django-admin-option:: --use-fuzzy, -f
  106. Includes fuzzy translations into compiled files.
  107. Example usage::
  108. django-admin compilemessages --locale=pt_BR
  109. django-admin compilemessages --locale=pt_BR --locale=fr -f
  110. django-admin compilemessages -l pt_BR
  111. django-admin compilemessages -l pt_BR -l fr --use-fuzzy
  112. django-admin compilemessages --exclude=pt_BR
  113. django-admin compilemessages --exclude=pt_BR --exclude=fr
  114. django-admin compilemessages -x pt_BR
  115. django-admin compilemessages -x pt_BR -x fr
  116. .. django-admin-option:: --ignore PATTERN, -i PATTERN
  117. .. versionadded:: 3.0
  118. Ignores directories matching the given :mod:`glob`-style pattern. Use
  119. multiple times to ignore more.
  120. Example usage::
  121. django-admin compilemessages --ignore=cache --ignore=outdated/*/locale
  122. ``createcachetable``
  123. --------------------
  124. .. django-admin:: createcachetable
  125. Creates the cache tables for use with the database cache backend using the
  126. information from your settings file. See :doc:`/topics/cache` for more
  127. information.
  128. .. django-admin-option:: --database DATABASE
  129. Specifies the database in which the cache table(s) will be created. Defaults to
  130. ``default``.
  131. .. django-admin-option:: --dry-run
  132. Prints the SQL that would be run without actually running it, so you can
  133. customize it or use the migrations framework.
  134. ``dbshell``
  135. -----------
  136. .. django-admin:: dbshell
  137. Runs the command-line client for the database engine specified in your
  138. :setting:`ENGINE <DATABASE-ENGINE>` setting, with the connection parameters
  139. specified in your :setting:`USER`, :setting:`PASSWORD`, etc., settings.
  140. * For PostgreSQL, this runs the ``psql`` command-line client.
  141. * For MySQL, this runs the ``mysql`` command-line client.
  142. * For SQLite, this runs the ``sqlite3`` command-line client.
  143. * For Oracle, this runs the ``sqlplus`` command-line client.
  144. This command assumes the programs are on your ``PATH`` so that a call to
  145. the program name (``psql``, ``mysql``, ``sqlite3``, ``sqlplus``) will find the
  146. program in the right place. There's no way to specify the location of the
  147. program manually.
  148. .. django-admin-option:: --database DATABASE
  149. Specifies the database onto which to open a shell. Defaults to ``default``.
  150. ``diffsettings``
  151. ----------------
  152. .. django-admin:: diffsettings
  153. Displays differences between the current settings file and Django's default
  154. settings (or another settings file specified by :option:`--default`).
  155. Settings that don't appear in the defaults are followed by ``"###"``. For
  156. example, the default settings don't define :setting:`ROOT_URLCONF`, so
  157. :setting:`ROOT_URLCONF` is followed by ``"###"`` in the output of
  158. ``diffsettings``.
  159. .. django-admin-option:: --all
  160. Displays all settings, even if they have Django's default value. Such settings
  161. are prefixed by ``"###"``.
  162. .. django-admin-option:: --default MODULE
  163. The settings module to compare the current settings against. Leave empty to
  164. compare against Django's default settings.
  165. .. django-admin-option:: --output {hash,unified}
  166. Specifies the output format. Available values are ``hash`` and ``unified``.
  167. ``hash`` is the default mode that displays the output that's described above.
  168. ``unified`` displays the output similar to ``diff -u``. Default settings are
  169. prefixed with a minus sign, followed by the changed setting prefixed with a
  170. plus sign.
  171. ``dumpdata``
  172. ------------
  173. .. django-admin:: dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]
  174. Outputs to standard output all data in the database associated with the named
  175. application(s).
  176. If no application name is provided, all installed applications will be dumped.
  177. The output of ``dumpdata`` can be used as input for :djadmin:`loaddata`.
  178. Note that ``dumpdata`` uses the default manager on the model for selecting the
  179. records to dump. If you're using a :ref:`custom manager <custom-managers>` as
  180. the default manager and it filters some of the available records, not all of the
  181. objects will be dumped.
  182. .. django-admin-option:: --all, -a
  183. Uses Django's base manager, dumping records which might otherwise be filtered
  184. or modified by a custom manager.
  185. .. django-admin-option:: --format FORMAT
  186. Specifies the serialization format of the output. Defaults to JSON. Supported
  187. formats are listed in :ref:`serialization-formats`.
  188. .. django-admin-option:: --indent INDENT
  189. Specifies the number of indentation spaces to use in the output. Defaults to
  190. ``None`` which displays all data on single line.
  191. .. django-admin-option:: --exclude EXCLUDE, -e EXCLUDE
  192. Prevents specific applications or models (specified in the form of
  193. ``app_label.ModelName``) from being dumped. If you specify a model name, the
  194. output will be restricted to that model, rather than the entire application.
  195. You can also mix application names and model names.
  196. If you want to exclude multiple applications, pass ``--exclude`` more than
  197. once::
  198. django-admin dumpdata --exclude=auth --exclude=contenttypes
  199. .. django-admin-option:: --database DATABASE
  200. Specifies the database from which data will be dumped. Defaults to ``default``.
  201. .. django-admin-option:: --natural-foreign
  202. Uses the ``natural_key()`` model method to serialize any foreign key and
  203. many-to-many relationship to objects of the type that defines the method. If
  204. you're dumping ``contrib.auth`` ``Permission`` objects or
  205. ``contrib.contenttypes`` ``ContentType`` objects, you should probably use this
  206. flag. See the :ref:`natural keys <topics-serialization-natural-keys>`
  207. documentation for more details on this and the next option.
  208. .. django-admin-option:: --natural-primary
  209. Omits the primary key in the serialized data of this object since it can be
  210. calculated during deserialization.
  211. .. django-admin-option:: --pks PRIMARY_KEYS
  212. Outputs only the objects specified by a comma separated list of primary keys.
  213. This is only available when dumping one model. By default, all the records of
  214. the model are output.
  215. .. django-admin-option:: --output OUTPUT, -o OUTPUT
  216. Specifies a file to write the serialized data to. By default, the data goes to
  217. standard output.
  218. When this option is set and ``--verbosity`` is greater than 0 (the default), a
  219. progress bar is shown in the terminal.
  220. ``flush``
  221. ---------
  222. .. django-admin:: flush
  223. Removes all data from the database and re-executes any post-synchronization
  224. handlers. The table of which migrations have been applied is not cleared.
  225. If you would rather start from an empty database and re-run all migrations, you
  226. should drop and recreate the database and then run :djadmin:`migrate` instead.
  227. .. django-admin-option:: --noinput, --no-input
  228. Suppresses all user prompts.
  229. .. django-admin-option:: --database DATABASE
  230. Specifies the database to flush. Defaults to ``default``.
  231. ``inspectdb``
  232. -------------
  233. .. django-admin:: inspectdb [table [table ...]]
  234. Introspects the database tables in the database pointed-to by the
  235. :setting:`NAME` setting and outputs a Django model module (a ``models.py``
  236. file) to standard output.
  237. You may choose what tables or views to inspect by passing their names as
  238. arguments. If no arguments are provided, models are created for views only if
  239. the :option:`--include-views` option is used. Models for partition tables are
  240. created on PostgreSQL if the :option:`--include-partitions` option is used.
  241. Use this if you have a legacy database with which you'd like to use Django.
  242. The script will inspect the database and create a model for each table within
  243. it.
  244. As you might expect, the created models will have an attribute for every field
  245. in the table. Note that ``inspectdb`` has a few special cases in its field-name
  246. output:
  247. * If ``inspectdb`` cannot map a column's type to a model field type, it'll
  248. use ``TextField`` and will insert the Python comment
  249. ``'This field type is a guess.'`` next to the field in the generated
  250. model. The recognized fields may depend on apps listed in
  251. :setting:`INSTALLED_APPS`. For example, :mod:`django.contrib.postgres` adds
  252. recognition for several PostgreSQL-specific field types.
  253. * If the database column name is a Python reserved word (such as
  254. ``'pass'``, ``'class'`` or ``'for'``), ``inspectdb`` will append
  255. ``'_field'`` to the attribute name. For example, if a table has a column
  256. ``'for'``, the generated model will have a field ``'for_field'``, with
  257. the ``db_column`` attribute set to ``'for'``. ``inspectdb`` will insert
  258. the Python comment
  259. ``'Field renamed because it was a Python reserved word.'`` next to the
  260. field.
  261. This feature is meant as a shortcut, not as definitive model generation. After
  262. you run it, you'll want to look over the generated models yourself to make
  263. customizations. In particular, you'll need to rearrange models' order, so that
  264. models that refer to other models are ordered properly.
  265. Django doesn't create database defaults when a
  266. :attr:`~django.db.models.Field.default` is specified on a model field.
  267. Similarly, database defaults aren't translated to model field defaults or
  268. detected in any fashion by ``inspectdb``.
  269. By default, ``inspectdb`` creates unmanaged models. That is, ``managed = False``
  270. in the model's ``Meta`` class tells Django not to manage each table's creation,
  271. modification, and deletion. If you do want to allow Django to manage the
  272. table's lifecycle, you'll need to change the
  273. :attr:`~django.db.models.Options.managed` option to ``True`` (or remove
  274. it because ``True`` is its default value).
  275. Database-specific notes
  276. ~~~~~~~~~~~~~~~~~~~~~~~
  277. Oracle
  278. ^^^^^^
  279. * Models are created for materialized views if :option:`--include-views` is
  280. used.
  281. PostgreSQL
  282. ^^^^^^^^^^
  283. * Models are created for foreign tables.
  284. * Models are created for materialized views if
  285. :option:`--include-views` is used.
  286. * Models are created for partition tables if
  287. :option:`--include-partitions` is used.
  288. .. django-admin-option:: --database DATABASE
  289. Specifies the database to introspect. Defaults to ``default``.
  290. .. django-admin-option:: --include-partitions
  291. If this option is provided, models are also created for partitions.
  292. Only support for PostgreSQL is implemented.
  293. .. django-admin-option:: --include-views
  294. If this option is provided, models are also created for database views.
  295. ``loaddata``
  296. ------------
  297. .. django-admin:: loaddata fixture [fixture ...]
  298. Searches for and loads the contents of the named fixture into the database.
  299. .. django-admin-option:: --database DATABASE
  300. Specifies the database into which the data will be loaded. Defaults to
  301. ``default``.
  302. .. django-admin-option:: --ignorenonexistent, -i
  303. Ignores fields and models that may have been removed since the fixture was
  304. originally generated.
  305. .. django-admin-option:: --app APP_LABEL
  306. Specifies a single app to look for fixtures in rather than looking in all apps.
  307. .. django-admin-option:: --format FORMAT
  308. Specifies the :ref:`serialization format <serialization-formats>` (e.g.,
  309. ``json`` or ``xml``) for fixtures :ref:`read from stdin
  310. <loading-fixtures-stdin>`.
  311. .. django-admin-option:: --exclude EXCLUDE, -e EXCLUDE
  312. Excludes loading the fixtures from the given applications and/or models (in the
  313. form of ``app_label`` or ``app_label.ModelName``). Use the option multiple
  314. times to exclude more than one app or model.
  315. What's a "fixture"?
  316. ~~~~~~~~~~~~~~~~~~~
  317. A *fixture* is a collection of files that contain the serialized contents of
  318. the database. Each fixture has a unique name, and the files that comprise the
  319. fixture can be distributed over multiple directories, in multiple applications.
  320. Django will search in three locations for fixtures:
  321. 1. In the ``fixtures`` directory of every installed application
  322. 2. In any directory named in the :setting:`FIXTURE_DIRS` setting
  323. 3. In the literal path named by the fixture
  324. Django will load any and all fixtures it finds in these locations that match
  325. the provided fixture names.
  326. If the named fixture has a file extension, only fixtures of that type
  327. will be loaded. For example::
  328. django-admin loaddata mydata.json
  329. would only load JSON fixtures called ``mydata``. The fixture extension
  330. must correspond to the registered name of a
  331. :ref:`serializer <serialization-formats>` (e.g., ``json`` or ``xml``).
  332. If you omit the extensions, Django will search all available fixture types
  333. for a matching fixture. For example::
  334. django-admin loaddata mydata
  335. would look for any fixture of any fixture type called ``mydata``. If a fixture
  336. directory contained ``mydata.json``, that fixture would be loaded
  337. as a JSON fixture.
  338. The fixtures that are named can include directory components. These
  339. directories will be included in the search path. For example::
  340. django-admin loaddata foo/bar/mydata.json
  341. would search ``<app_label>/fixtures/foo/bar/mydata.json`` for each installed
  342. application, ``<dirname>/foo/bar/mydata.json`` for each directory in
  343. :setting:`FIXTURE_DIRS`, and the literal path ``foo/bar/mydata.json``.
  344. When fixture files are processed, the data is saved to the database as is.
  345. Model defined :meth:`~django.db.models.Model.save` methods are not called, and
  346. any :data:`~django.db.models.signals.pre_save` or
  347. :data:`~django.db.models.signals.post_save` signals will be called with
  348. ``raw=True`` since the instance only contains attributes that are local to the
  349. model. You may, for example, want to disable handlers that access
  350. related fields that aren't present during fixture loading and would otherwise
  351. raise an exception::
  352. from django.db.models.signals import post_save
  353. from .models import MyModel
  354. def my_handler(**kwargs):
  355. # disable the handler during fixture loading
  356. if kwargs['raw']:
  357. return
  358. ...
  359. post_save.connect(my_handler, sender=MyModel)
  360. You could also write a decorator to encapsulate this logic::
  361. from functools import wraps
  362. def disable_for_loaddata(signal_handler):
  363. """
  364. Decorator that turns off signal handlers when loading fixture data.
  365. """
  366. @wraps(signal_handler)
  367. def wrapper(*args, **kwargs):
  368. if kwargs['raw']:
  369. return
  370. signal_handler(*args, **kwargs)
  371. return wrapper
  372. @disable_for_loaddata
  373. def my_handler(**kwargs):
  374. ...
  375. Just be aware that this logic will disable the signals whenever fixtures are
  376. deserialized, not just during ``loaddata``.
  377. Note that the order in which fixture files are processed is undefined. However,
  378. all fixture data is installed as a single transaction, so data in
  379. one fixture can reference data in another fixture. If the database backend
  380. supports row-level constraints, these constraints will be checked at the
  381. end of the transaction.
  382. The :djadmin:`dumpdata` command can be used to generate input for ``loaddata``.
  383. Compressed fixtures
  384. ~~~~~~~~~~~~~~~~~~~
  385. Fixtures may be compressed in ``zip``, ``gz``, or ``bz2`` format. For example::
  386. django-admin loaddata mydata.json
  387. would look for any of ``mydata.json``, ``mydata.json.zip``,
  388. ``mydata.json.gz``, or ``mydata.json.bz2``. The first file contained within a
  389. zip-compressed archive is used.
  390. Note that if two fixtures with the same name but different
  391. fixture type are discovered (for example, if ``mydata.json`` and
  392. ``mydata.xml.gz`` were found in the same fixture directory), fixture
  393. installation will be aborted, and any data installed in the call to
  394. ``loaddata`` will be removed from the database.
  395. .. admonition:: MySQL with MyISAM and fixtures
  396. The MyISAM storage engine of MySQL doesn't support transactions or
  397. constraints, so if you use MyISAM, you won't get validation of fixture
  398. data, or a rollback if multiple transaction files are found.
  399. Database-specific fixtures
  400. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  401. If you're in a multi-database setup, you might have fixture data that
  402. you want to load onto one database, but not onto another. In this
  403. situation, you can add a database identifier into the names of your fixtures.
  404. For example, if your :setting:`DATABASES` setting has a 'master' database
  405. defined, name the fixture ``mydata.master.json`` or
  406. ``mydata.master.json.gz`` and the fixture will only be loaded when you
  407. specify you want to load data into the ``master`` database.
  408. .. _loading-fixtures-stdin:
  409. Loading fixtures from ``stdin``
  410. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  411. You can use a dash as the fixture name to load input from ``sys.stdin``. For
  412. example::
  413. django-admin loaddata --format=json -
  414. When reading from ``stdin``, the :option:`--format <loaddata --format>` option
  415. is required to specify the :ref:`serialization format <serialization-formats>`
  416. of the input (e.g., ``json`` or ``xml``).
  417. Loading from ``stdin`` is useful with standard input and output redirections.
  418. For example::
  419. django-admin dumpdata --format=json --database=test app_label.ModelName | django-admin loaddata --format=json --database=prod -
  420. ``makemessages``
  421. ----------------
  422. .. django-admin:: makemessages
  423. Runs over the entire source tree of the current directory and pulls out all
  424. strings marked for translation. It creates (or updates) a message file in the
  425. conf/locale (in the Django tree) or locale (for project and application)
  426. directory. After making changes to the messages files you need to compile them
  427. with :djadmin:`compilemessages` for use with the builtin gettext support. See
  428. the :ref:`i18n documentation <how-to-create-language-files>` for details.
  429. This command doesn't require configured settings. However, when settings aren't
  430. configured, the command can't ignore the :setting:`MEDIA_ROOT` and
  431. :setting:`STATIC_ROOT` directories or include :setting:`LOCALE_PATHS`.
  432. .. django-admin-option:: --all, -a
  433. Updates the message files for all available languages.
  434. .. django-admin-option:: --extension EXTENSIONS, -e EXTENSIONS
  435. Specifies a list of file extensions to examine (default: ``html``, ``txt``,
  436. ``py`` or ``js`` if :option:`--domain` is ``js``).
  437. Example usage::
  438. django-admin makemessages --locale=de --extension xhtml
  439. Separate multiple extensions with commas or use ``-e`` or ``--extension``
  440. multiple times::
  441. django-admin makemessages --locale=de --extension=html,txt --extension xml
  442. .. django-admin-option:: --locale LOCALE, -l LOCALE
  443. Specifies the locale(s) to process.
  444. .. django-admin-option:: --exclude EXCLUDE, -x EXCLUDE
  445. Specifies the locale(s) to exclude from processing. If not provided, no locales
  446. are excluded.
  447. Example usage::
  448. django-admin makemessages --locale=pt_BR
  449. django-admin makemessages --locale=pt_BR --locale=fr
  450. django-admin makemessages -l pt_BR
  451. django-admin makemessages -l pt_BR -l fr
  452. django-admin makemessages --exclude=pt_BR
  453. django-admin makemessages --exclude=pt_BR --exclude=fr
  454. django-admin makemessages -x pt_BR
  455. django-admin makemessages -x pt_BR -x fr
  456. .. django-admin-option:: --domain DOMAIN, -d DOMAIN
  457. Specifies the domain of the messages files. Supported options are:
  458. * ``django`` for all ``*.py``, ``*.html`` and ``*.txt`` files (default)
  459. * ``djangojs`` for ``*.js`` files
  460. .. django-admin-option:: --symlinks, -s
  461. Follows symlinks to directories when looking for new translation strings.
  462. Example usage::
  463. django-admin makemessages --locale=de --symlinks
  464. .. django-admin-option:: --ignore PATTERN, -i PATTERN
  465. Ignores files or directories matching the given :mod:`glob`-style pattern. Use
  466. multiple times to ignore more.
  467. These patterns are used by default: ``'CVS'``, ``'.*'``, ``'*~'``, ``'*.pyc'``.
  468. Example usage::
  469. django-admin makemessages --locale=en_US --ignore=apps/* --ignore=secret/*.html
  470. .. django-admin-option:: --no-default-ignore
  471. Disables the default values of ``--ignore``.
  472. .. django-admin-option:: --no-wrap
  473. Disables breaking long message lines into several lines in language files.
  474. .. django-admin-option:: --no-location
  475. Suppresses writing '``#: filename:line``’ comment lines in language files.
  476. Using this option makes it harder for technically skilled translators to
  477. understand each message's context.
  478. .. django-admin-option:: --add-location [{full,file,never}]
  479. Controls ``#: filename:line`` comment lines in language files. If the option
  480. is:
  481. * ``full`` (the default if not given): the lines include both file name and
  482. line number.
  483. * ``file``: the line number is omitted.
  484. * ``never``: the lines are suppressed (same as :option:`--no-location`).
  485. Requires ``gettext`` 0.19 or newer.
  486. .. django-admin-option:: --keep-pot
  487. Prevents deleting the temporary ``.pot`` files generated before creating the
  488. ``.po`` file. This is useful for debugging errors which may prevent the final
  489. language files from being created.
  490. .. seealso::
  491. See :ref:`customizing-makemessages` for instructions on how to customize
  492. the keywords that :djadmin:`makemessages` passes to ``xgettext``.
  493. ``makemigrations``
  494. ------------------
  495. .. django-admin:: makemigrations [app_label [app_label ...]]
  496. Creates new migrations based on the changes detected to your models.
  497. Migrations, their relationship with apps and more are covered in depth in
  498. :doc:`the migrations documentation</topics/migrations>`.
  499. Providing one or more app names as arguments will limit the migrations created
  500. to the app(s) specified and any dependencies needed (the table at the other end
  501. of a ``ForeignKey``, for example).
  502. To add migrations to an app that doesn't have a ``migrations`` directory, run
  503. ``makemigrations`` with the app's ``app_label``.
  504. .. django-admin-option:: --noinput, --no-input
  505. Suppresses all user prompts. If a suppressed prompt cannot be resolved
  506. automatically, the command will exit with error code 3.
  507. .. django-admin-option:: --empty
  508. Outputs an empty migration for the specified apps, for manual editing. This is
  509. for advanced users and should not be used unless you are familiar with the
  510. migration format, migration operations, and the dependencies between your
  511. migrations.
  512. .. django-admin-option:: --dry-run
  513. Shows what migrations would be made without actually writing any migrations
  514. files to disk. Using this option along with ``--verbosity 3`` will also show
  515. the complete migrations files that would be written.
  516. .. django-admin-option:: --merge
  517. Enables fixing of migration conflicts.
  518. .. django-admin-option:: --name NAME, -n NAME
  519. Allows naming the generated migration(s) instead of using a generated name. The
  520. name must be a valid Python :ref:`identifier <python:identifiers>`.
  521. .. django-admin-option:: --no-header
  522. Generate migration files without Django version and timestamp header.
  523. .. django-admin-option:: --check
  524. Makes ``makemigrations`` exit with a non-zero status when model changes without
  525. migrations are detected.
  526. ``migrate``
  527. -----------
  528. .. django-admin:: migrate [app_label] [migration_name]
  529. Synchronizes the database state with the current set of models and migrations.
  530. Migrations, their relationship with apps and more are covered in depth in
  531. :doc:`the migrations documentation</topics/migrations>`.
  532. The behavior of this command changes depending on the arguments provided:
  533. * No arguments: All apps have all of their migrations run.
  534. * ``<app_label>``: The specified app has its migrations run, up to the most
  535. recent migration. This may involve running other apps' migrations too, due
  536. to dependencies.
  537. * ``<app_label> <migrationname>``: Brings the database schema to a state where
  538. the named migration is applied, but no later migrations in the same app are
  539. applied. This may involve unapplying migrations if you have previously
  540. migrated past the named migration. You can use a prefix of the migration
  541. name, e.g. ``0001``, as long as it's unique for the given app name. Use the
  542. name ``zero`` to migrate all the way back i.e. to revert all applied
  543. migrations for an app.
  544. .. warning::
  545. When unapplying migrations, all dependent migrations will also be
  546. unapplied, regardless of ``<app_label>``. You can use ``--plan`` to check
  547. which migrations will be unapplied.
  548. .. django-admin-option:: --database DATABASE
  549. Specifies the database to migrate. Defaults to ``default``.
  550. .. django-admin-option:: --fake
  551. Marks the migrations up to the target one (following the rules above) as
  552. applied, but without actually running the SQL to change your database schema.
  553. This is intended for advanced users to manipulate the
  554. current migration state directly if they're manually applying changes;
  555. be warned that using ``--fake`` runs the risk of putting the migration state
  556. table into a state where manual recovery will be needed to make migrations
  557. run correctly.
  558. .. django-admin-option:: --fake-initial
  559. Allows Django to skip an app's initial migration if all database tables with
  560. the names of all models created by all
  561. :class:`~django.db.migrations.operations.CreateModel` operations in that
  562. migration already exist. This option is intended for use when first running
  563. migrations against a database that preexisted the use of migrations. This
  564. option does not, however, check for matching database schema beyond matching
  565. table names and so is only safe to use if you are confident that your existing
  566. schema matches what is recorded in your initial migration.
  567. .. django-admin-option:: --plan
  568. Shows the migration operations that will be performed for the given ``migrate``
  569. command.
  570. .. django-admin-option:: --run-syncdb
  571. Allows creating tables for apps without migrations. While this isn't
  572. recommended, the migrations framework is sometimes too slow on large projects
  573. with hundreds of models.
  574. .. django-admin-option:: --noinput, --no-input
  575. Suppresses all user prompts. An example prompt is asking about removing stale
  576. content types.
  577. ``runserver``
  578. -------------
  579. .. django-admin:: runserver [addrport]
  580. Starts a lightweight development Web server on the local machine. By default,
  581. the server runs on port 8000 on the IP address ``127.0.0.1``. You can pass in an
  582. IP address and port number explicitly.
  583. If you run this script as a user with normal privileges (recommended), you
  584. might not have access to start a port on a low port number. Low port numbers
  585. are reserved for the superuser (root).
  586. This server uses the WSGI application object specified by the
  587. :setting:`WSGI_APPLICATION` setting.
  588. DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through
  589. security audits or performance tests. (And that's how it's gonna stay. We're in
  590. the business of making Web frameworks, not Web servers, so improving this
  591. server to be able to handle a production environment is outside the scope of
  592. Django.)
  593. The development server automatically reloads Python code for each request, as
  594. needed. You don't need to restart the server for code changes to take effect.
  595. However, some actions like adding files don't trigger a restart, so you'll
  596. have to restart the server in these cases.
  597. If you're using Linux or MacOS and install both `pywatchman`_ and the
  598. `Watchman`_ service, kernel signals will be used to autoreload the server
  599. (rather than polling file modification timestamps each second). This offers
  600. better performance on large projects, reduced response time after code changes,
  601. more robust change detection, and a reduction in power usage. Django supports
  602. ``pywatchman`` 1.2.0 and higher.
  603. .. admonition:: Large directories with many files may cause performance issues
  604. When using Watchman with a project that includes large non-Python
  605. directories like ``node_modules``, it's advisable to ignore this directory
  606. for optimal performance. See the `watchman documentation`_ for information
  607. on how to do this.
  608. .. admonition:: Watchman timeout
  609. The default timeout of ``Watchman`` client is 5 seconds. You can change it
  610. by setting the ``DJANGO_WATCHMAN_TIMEOUT`` environment variable.
  611. .. _Watchman: https://facebook.github.io/watchman/
  612. .. _pywatchman: https://pypi.org/project/pywatchman/
  613. .. _watchman documentation: https://facebook.github.io/watchman/docs/config.html#ignore_dirs
  614. When you start the server, and each time you change Python code while the
  615. server is running, the system check framework will check your entire Django
  616. project for some common errors (see the :djadmin:`check` command). If any
  617. errors are found, they will be printed to standard output.
  618. You can run as many concurrent servers as you want, as long as they're on
  619. separate ports by executing ``django-admin runserver`` more than once.
  620. Note that the default IP address, ``127.0.0.1``, is not accessible from other
  621. machines on your network. To make your development server viewable to other
  622. machines on the network, use its own IP address (e.g. ``192.168.2.1``) or
  623. ``0.0.0.0`` or ``::`` (with IPv6 enabled).
  624. You can provide an IPv6 address surrounded by brackets
  625. (e.g. ``[200a::1]:8000``). This will automatically enable IPv6 support.
  626. A hostname containing ASCII-only characters can also be used.
  627. If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled
  628. (default in new projects) the :djadmin:`runserver` command will be overridden
  629. with its own :ref:`runserver<staticfiles-runserver>` command.
  630. Logging of each request and response of the server is sent to the
  631. :ref:`django-server-logger` logger.
  632. .. django-admin-option:: --noreload
  633. Disables the auto-reloader. This means any Python code changes you make while
  634. the server is running will *not* take effect if the particular Python modules
  635. have already been loaded into memory.
  636. .. django-admin-option:: --nothreading
  637. Disables use of threading in the development server. The server is
  638. multithreaded by default.
  639. .. django-admin-option:: --ipv6, -6
  640. Uses IPv6 for the development server. This changes the default IP address from
  641. ``127.0.0.1`` to ``::1``.
  642. Examples of using different ports and addresses
  643. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  644. Port 8000 on IP address ``127.0.0.1``::
  645. django-admin runserver
  646. Port 8000 on IP address ``1.2.3.4``::
  647. django-admin runserver 1.2.3.4:8000
  648. Port 7000 on IP address ``127.0.0.1``::
  649. django-admin runserver 7000
  650. Port 7000 on IP address ``1.2.3.4``::
  651. django-admin runserver 1.2.3.4:7000
  652. Port 8000 on IPv6 address ``::1``::
  653. django-admin runserver -6
  654. Port 7000 on IPv6 address ``::1``::
  655. django-admin runserver -6 7000
  656. Port 7000 on IPv6 address ``2001:0db8:1234:5678::9``::
  657. django-admin runserver [2001:0db8:1234:5678::9]:7000
  658. Port 8000 on IPv4 address of host ``localhost``::
  659. django-admin runserver localhost:8000
  660. Port 8000 on IPv6 address of host ``localhost``::
  661. django-admin runserver -6 localhost:8000
  662. Serving static files with the development server
  663. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  664. By default, the development server doesn't serve any static files for your site
  665. (such as CSS files, images, things under :setting:`MEDIA_URL` and so forth). If
  666. you want to configure Django to serve static media, read
  667. :doc:`/howto/static-files/index`.
  668. ``sendtestemail``
  669. -----------------
  670. .. django-admin:: sendtestemail [email [email ...]]
  671. Sends a test email (to confirm email sending through Django is working) to the
  672. recipient(s) specified. For example::
  673. django-admin sendtestemail foo@example.com bar@example.com
  674. There are a couple of options, and you may use any combination of them
  675. together:
  676. .. django-admin-option:: --managers
  677. Mails the email addresses specified in :setting:`MANAGERS` using
  678. :meth:`~django.core.mail.mail_managers()`.
  679. .. django-admin-option:: --admins
  680. Mails the email addresses specified in :setting:`ADMINS` using
  681. :meth:`~django.core.mail.mail_admins()`.
  682. ``shell``
  683. ---------
  684. .. django-admin:: shell
  685. Starts the Python interactive interpreter.
  686. .. django-admin-option:: --interface {ipython,bpython,python}, -i {ipython,bpython,python}
  687. Specifies the shell to use. By default, Django will use IPython_ or bpython_ if
  688. either is installed. If both are installed, specify which one you want like so:
  689. IPython::
  690. django-admin shell -i ipython
  691. bpython::
  692. django-admin shell -i bpython
  693. If you have a "rich" shell installed but want to force use of the "plain"
  694. Python interpreter, use ``python`` as the interface name, like so::
  695. django-admin shell -i python
  696. .. _IPython: https://ipython.org/
  697. .. _bpython: https://bpython-interpreter.org/
  698. .. django-admin-option:: --nostartup
  699. Disables reading the startup script for the "plain" Python interpreter. By
  700. default, the script pointed to by the :envvar:`PYTHONSTARTUP` environment
  701. variable or the ``~/.pythonrc.py`` script is read.
  702. .. django-admin-option:: --command COMMAND, -c COMMAND
  703. Lets you pass a command as a string to execute it as Django, like so::
  704. django-admin shell --command="import django; print(django.__version__)"
  705. You can also pass code in on standard input to execute it. For example:
  706. .. code-block:: console
  707. $ django-admin shell <<EOF
  708. > import django
  709. > print(django.__version__)
  710. > EOF
  711. On Windows, the REPL is output due to implementation limits of
  712. :func:`select.select` on that platform.
  713. ``showmigrations``
  714. ------------------
  715. .. django-admin:: showmigrations [app_label [app_label ...]]
  716. Shows all migrations in a project. You can choose from one of two formats:
  717. .. django-admin-option:: --list, -l
  718. Lists all of the apps Django knows about, the migrations available for each
  719. app, and whether or not each migration is applied (marked by an ``[X]`` next to
  720. the migration name). For a ``--verbosity`` of 2 and above, the applied
  721. datetimes are also shown.
  722. Apps without migrations are also listed, but have ``(no migrations)`` printed
  723. under them.
  724. This is the default output format.
  725. .. versionchanged:: 3.0
  726. Output of the applied datetimes at verbosity 2 and above was added.
  727. .. django-admin-option:: --plan, -p
  728. Shows the migration plan Django will follow to apply migrations. Like
  729. ``--list``, applied migrations are marked by an ``[X]``. For a ``--verbosity``
  730. of 2 and above, all dependencies of a migration will also be shown.
  731. ``app_label``\s arguments limit the output, however, dependencies of provided
  732. apps may also be included.
  733. .. django-admin-option:: --database DATABASE
  734. Specifies the database to examine. Defaults to ``default``.
  735. ``sqlflush``
  736. ------------
  737. .. django-admin:: sqlflush
  738. Prints the SQL statements that would be executed for the :djadmin:`flush`
  739. command.
  740. .. django-admin-option:: --database DATABASE
  741. Specifies the database for which to print the SQL. Defaults to ``default``.
  742. ``sqlmigrate``
  743. --------------
  744. .. django-admin:: sqlmigrate app_label migration_name
  745. Prints the SQL for the named migration. This requires an active database
  746. connection, which it will use to resolve constraint names; this means you must
  747. generate the SQL against a copy of the database you wish to later apply it on.
  748. Note that ``sqlmigrate`` doesn't colorize its output.
  749. .. django-admin-option:: --backwards
  750. Generates the SQL for unapplying the migration. By default, the SQL created is
  751. for running the migration in the forwards direction.
  752. .. django-admin-option:: --database DATABASE
  753. Specifies the database for which to generate the SQL. Defaults to ``default``.
  754. ``sqlsequencereset``
  755. --------------------
  756. .. django-admin:: sqlsequencereset app_label [app_label ...]
  757. Prints the SQL statements for resetting sequences for the given app name(s).
  758. Sequences are indexes used by some database engines to track the next available
  759. number for automatically incremented fields.
  760. Use this command to generate SQL which will fix cases where a sequence is out
  761. of sync with its automatically incremented field data.
  762. .. django-admin-option:: --database DATABASE
  763. Specifies the database for which to print the SQL. Defaults to ``default``.
  764. ``squashmigrations``
  765. --------------------
  766. .. django-admin:: squashmigrations app_label [start_migration_name] migration_name
  767. Squashes the migrations for ``app_label`` up to and including ``migration_name``
  768. down into fewer migrations, if possible. The resulting squashed migrations
  769. can live alongside the unsquashed ones safely. For more information,
  770. please read :ref:`migration-squashing`.
  771. When ``start_migration_name`` is given, Django will only include migrations
  772. starting from and including this migration. This helps to mitigate the
  773. squashing limitation of :class:`~django.db.migrations.operations.RunPython` and
  774. :class:`django.db.migrations.operations.RunSQL` migration operations.
  775. .. django-admin-option:: --no-optimize
  776. Disables the optimizer when generating a squashed migration. By default, Django
  777. will try to optimize the operations in your migrations to reduce the size of
  778. the resulting file. Use this option if this process is failing or creating
  779. incorrect migrations, though please also file a Django bug report about the
  780. behavior, as optimization is meant to be safe.
  781. .. django-admin-option:: --noinput, --no-input
  782. Suppresses all user prompts.
  783. .. django-admin-option:: --squashed-name SQUASHED_NAME
  784. Sets the name of the squashed migration. When omitted, the name is based on the
  785. first and last migration, with ``_squashed_`` in between.
  786. .. django-admin-option:: --no-header
  787. Generate squashed migration file without Django version and timestamp header.
  788. ``startapp``
  789. ------------
  790. .. django-admin:: startapp name [directory]
  791. Creates a Django app directory structure for the given app name in the current
  792. directory or the given destination.
  793. By default, :source:`the new directory <django/conf/app_template>` contains a
  794. ``models.py`` file and other app template files. If only the app name is given,
  795. the app directory will be created in the current working directory.
  796. If the optional destination is provided, Django will use that existing
  797. directory rather than creating a new one. You can use '.' to denote the current
  798. working directory.
  799. For example::
  800. django-admin startapp myapp /Users/jezdez/Code/myapp
  801. .. _custom-app-and-project-templates:
  802. .. django-admin-option:: --template TEMPLATE
  803. Provides the path to a directory with a custom app template file, or a path to
  804. an uncompressed archive (``.tar``) or a compressed archive (``.tar.gz``,
  805. ``.tar.bz2``, ``.tar.xz``, ``.tar.lzma``, ``.tgz``, ``.tbz2``, ``.txz``,
  806. ``.tlz``, ``.zip``) containing the app template files.
  807. For example, this would look for an app template in the given directory when
  808. creating the ``myapp`` app::
  809. django-admin startapp --template=/Users/jezdez/Code/my_app_template myapp
  810. Django will also accept URLs (``http``, ``https``, ``ftp``) to compressed
  811. archives with the app template files, downloading and extracting them on the
  812. fly.
  813. For example, taking advantage of GitHub's feature to expose repositories as
  814. zip files, you can use a URL like::
  815. django-admin startapp --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp
  816. .. versionchanged:: 3.0
  817. Support for XZ archives (``.tar.xz``, ``.txz``) and LZMA archives
  818. (``.tar.lzma``, ``.tlz``) was added.
  819. .. django-admin-option:: --extension EXTENSIONS, -e EXTENSIONS
  820. Specifies which file extensions in the app template should be rendered with the
  821. template engine. Defaults to ``py``.
  822. .. django-admin-option:: --name FILES, -n FILES
  823. Specifies which files in the app template (in addition to those matching
  824. ``--extension``) should be rendered with the template engine. Defaults to an
  825. empty list.
  826. The :class:`template context <django.template.Context>` used for all matching
  827. files is:
  828. - Any option passed to the ``startapp`` command (among the command's supported
  829. options)
  830. - ``app_name`` -- the app name as passed to the command
  831. - ``app_directory`` -- the full path of the newly created app
  832. - ``camel_case_app_name`` -- the app name in camel case format
  833. - ``docs_version`` -- the version of the documentation: ``'dev'`` or ``'1.x'``
  834. - ``django_version`` -- the version of Django, e.g. ``'2.0.3'``
  835. .. _render_warning:
  836. .. warning::
  837. When the app template files are rendered with the Django template
  838. engine (by default all ``*.py`` files), Django will also replace all
  839. stray template variables contained. For example, if one of the Python files
  840. contains a docstring explaining a particular feature related
  841. to template rendering, it might result in an incorrect example.
  842. To work around this problem, you can use the :ttag:`templatetag`
  843. template tag to "escape" the various parts of the template syntax.
  844. In addition, to allow Python template files that contain Django template
  845. language syntax while also preventing packaging systems from trying to
  846. byte-compile invalid ``*.py`` files, template files ending with ``.py-tpl``
  847. will be renamed to ``.py``.
  848. ``startproject``
  849. ----------------
  850. .. django-admin:: startproject name [directory]
  851. Creates a Django project directory structure for the given project name in
  852. the current directory or the given destination.
  853. By default, :source:`the new directory <django/conf/project_template>` contains
  854. ``manage.py`` and a project package (containing a ``settings.py`` and other
  855. files).
  856. If only the project name is given, both the project directory and project
  857. package will be named ``<projectname>`` and the project directory
  858. will be created in the current working directory.
  859. If the optional destination is provided, Django will use that existing
  860. directory as the project directory, and create ``manage.py`` and the project
  861. package within it. Use '.' to denote the current working directory.
  862. For example::
  863. django-admin startproject myproject /Users/jezdez/Code/myproject_repo
  864. .. django-admin-option:: --template TEMPLATE
  865. Specifies a directory, file path, or URL of a custom project template. See the
  866. :option:`startapp --template` documentation for examples and usage.
  867. .. django-admin-option:: --extension EXTENSIONS, -e EXTENSIONS
  868. Specifies which file extensions in the project template should be rendered with
  869. the template engine. Defaults to ``py``.
  870. .. django-admin-option:: --name FILES, -n FILES
  871. Specifies which files in the project template (in addition to those matching
  872. ``--extension``) should be rendered with the template engine. Defaults to an
  873. empty list.
  874. The :class:`template context <django.template.Context>` used is:
  875. - Any option passed to the ``startproject`` command (among the command's
  876. supported options)
  877. - ``project_name`` -- the project name as passed to the command
  878. - ``project_directory`` -- the full path of the newly created project
  879. - ``secret_key`` -- a random key for the :setting:`SECRET_KEY` setting
  880. - ``docs_version`` -- the version of the documentation: ``'dev'`` or ``'1.x'``
  881. - ``django_version`` -- the version of Django, e.g. ``'2.0.3'``
  882. Please also see the :ref:`rendering warning <render_warning>` as mentioned
  883. for :djadmin:`startapp`.
  884. ``test``
  885. --------
  886. .. django-admin:: test [test_label [test_label ...]]
  887. Runs tests for all installed apps. See :doc:`/topics/testing/index` for more
  888. information.
  889. .. django-admin-option:: --failfast
  890. Stops running tests and reports the failure immediately after a test fails.
  891. .. django-admin-option:: --testrunner TESTRUNNER
  892. Controls the test runner class that is used to execute tests. This value
  893. overrides the value provided by the :setting:`TEST_RUNNER` setting.
  894. .. django-admin-option:: --noinput, --no-input
  895. Suppresses all user prompts. A typical prompt is a warning about deleting an
  896. existing test database.
  897. Test runner options
  898. ~~~~~~~~~~~~~~~~~~~
  899. The ``test`` command receives options on behalf of the specified
  900. :option:`--testrunner`. These are the options of the default test runner:
  901. :class:`~django.test.runner.DiscoverRunner`.
  902. .. django-admin-option:: --keepdb
  903. Preserves the test database between test runs. This has the advantage of
  904. skipping both the create and destroy actions which can greatly decrease the
  905. time to run tests, especially those in a large test suite. If the test database
  906. does not exist, it will be created on the first run and then preserved for each
  907. subsequent run. Any unapplied migrations will also be applied to the test
  908. database before running the test suite.
  909. .. django-admin-option:: --reverse, -r
  910. Sorts test cases in the opposite execution order. This may help in debugging
  911. the side effects of tests that aren't properly isolated. :ref:`Grouping by test
  912. class <order-of-tests>` is preserved when using this option.
  913. .. django-admin-option:: --debug-mode
  914. Sets the :setting:`DEBUG` setting to ``True`` prior to running tests. This may
  915. help troubleshoot test failures.
  916. .. django-admin-option:: --debug-sql, -d
  917. Enables :ref:`SQL logging <django-db-logger>` for failing tests. If
  918. ``--verbosity`` is ``2``, then queries in passing tests are also output.
  919. .. django-admin-option:: --parallel [N]
  920. Runs tests in separate parallel processes. Since modern processors have
  921. multiple cores, this allows running tests significantly faster.
  922. By default ``--parallel`` runs one process per core according to
  923. :func:`multiprocessing.cpu_count()`. You can adjust the number of processes
  924. either by providing it as the option's value, e.g. ``--parallel=4``, or by
  925. setting the ``DJANGO_TEST_PROCESSES`` environment variable.
  926. Django distributes test cases — :class:`unittest.TestCase` subclasses — to
  927. subprocesses. If there are fewer test cases than configured processes, Django
  928. will reduce the number of processes accordingly.
  929. Each process gets its own database. You must ensure that different test cases
  930. don't access the same resources. For instance, test cases that touch the
  931. filesystem should create a temporary directory for their own use.
  932. .. note::
  933. If you have test classes that cannot be run in parallel, you can use
  934. ``SerializeMixin`` to run them sequentially. See :ref:`Enforce running test
  935. classes sequentially <topics-testing-enforce-run-sequentially>`.
  936. This option requires the third-party ``tblib`` package to display tracebacks
  937. correctly:
  938. .. code-block:: console
  939. $ python -m pip install tblib
  940. This feature isn't available on Windows. It doesn't work with the Oracle
  941. database backend either.
  942. If you want to use :mod:`pdb` while debugging tests, you must disable parallel
  943. execution (``--parallel=1``). You'll see something like ``bdb.BdbQuit`` if you
  944. don't.
  945. .. warning::
  946. When test parallelization is enabled and a test fails, Django may be
  947. unable to display the exception traceback. This can make debugging
  948. difficult. If you encounter this problem, run the affected test without
  949. parallelization to see the traceback of the failure.
  950. This is a known limitation. It arises from the need to serialize objects
  951. in order to exchange them between processes. See
  952. :ref:`python:pickle-picklable` for details.
  953. .. option:: --tag TAGS
  954. Runs only tests :ref:`marked with the specified tags <topics-tagging-tests>`.
  955. May be specified multiple times and combined with :option:`test --exclude-tag`.
  956. .. option:: --exclude-tag EXCLUDE_TAGS
  957. Excludes tests :ref:`marked with the specified tags <topics-tagging-tests>`.
  958. May be specified multiple times and combined with :option:`test --tag`.
  959. .. django-admin-option:: -k TEST_NAME_PATTERNS
  960. .. versionadded:: 3.0
  961. Runs test methods and classes matching test name patterns, in the same way as
  962. :option:`unittest's -k option<unittest.-k>`. Can be specified multiple times.
  963. .. admonition:: Python 3.7 and later
  964. This feature is only available for Python 3.7 and later.
  965. .. django-admin-option:: --pdb
  966. .. versionadded:: 3.0
  967. Spawns a ``pdb`` debugger at each test error or failure. If you have it
  968. installed, ``ipdb`` is used instead.
  969. ``testserver``
  970. --------------
  971. .. django-admin:: testserver [fixture [fixture ...]]
  972. Runs a Django development server (as in :djadmin:`runserver`) using data from
  973. the given fixture(s).
  974. For example, this command::
  975. django-admin testserver mydata.json
  976. ...would perform the following steps:
  977. #. Create a test database, as described in :ref:`the-test-database`.
  978. #. Populate the test database with fixture data from the given fixtures.
  979. (For more on fixtures, see the documentation for :djadmin:`loaddata` above.)
  980. #. Runs the Django development server (as in :djadmin:`runserver`), pointed at
  981. this newly created test database instead of your production database.
  982. This is useful in a number of ways:
  983. * When you're writing :doc:`unit tests </topics/testing/overview>` of how your views
  984. act with certain fixture data, you can use ``testserver`` to interact with
  985. the views in a Web browser, manually.
  986. * Let's say you're developing your Django application and have a "pristine"
  987. copy of a database that you'd like to interact with. You can dump your
  988. database to a fixture (using the :djadmin:`dumpdata` command, explained
  989. above), then use ``testserver`` to run your Web application with that data.
  990. With this arrangement, you have the flexibility of messing up your data
  991. in any way, knowing that whatever data changes you're making are only
  992. being made to a test database.
  993. Note that this server does *not* automatically detect changes to your Python
  994. source code (as :djadmin:`runserver` does). It does, however, detect changes to
  995. templates.
  996. .. django-admin-option:: --addrport ADDRPORT
  997. Specifies a different port, or IP address and port, from the default of
  998. ``127.0.0.1:8000``. This value follows exactly the same format and serves
  999. exactly the same function as the argument to the :djadmin:`runserver` command.
  1000. Examples:
  1001. To run the test server on port 7000 with ``fixture1`` and ``fixture2``::
  1002. django-admin testserver --addrport 7000 fixture1 fixture2
  1003. django-admin testserver fixture1 fixture2 --addrport 7000
  1004. (The above statements are equivalent. We include both of them to demonstrate
  1005. that it doesn't matter whether the options come before or after the fixture
  1006. arguments.)
  1007. To run on 1.2.3.4:7000 with a ``test`` fixture::
  1008. django-admin testserver --addrport 1.2.3.4:7000 test
  1009. .. django-admin-option:: --noinput, --no-input
  1010. Suppresses all user prompts. A typical prompt is a warning about deleting an
  1011. existing test database.
  1012. Commands provided by applications
  1013. =================================
  1014. Some commands are only available when the ``django.contrib`` application that
  1015. :doc:`implements </howto/custom-management-commands>` them has been
  1016. :setting:`enabled <INSTALLED_APPS>`. This section describes them grouped by
  1017. their application.
  1018. ``django.contrib.auth``
  1019. -----------------------
  1020. ``changepassword``
  1021. ~~~~~~~~~~~~~~~~~~
  1022. .. django-admin:: changepassword [<username>]
  1023. This command is only available if Django's :doc:`authentication system
  1024. </topics/auth/index>` (``django.contrib.auth``) is installed.
  1025. Allows changing a user's password. It prompts you to enter a new password twice
  1026. for the given user. If the entries are identical, this immediately becomes the
  1027. new password. If you do not supply a user, the command will attempt to change
  1028. the password whose username matches the current user.
  1029. .. django-admin-option:: --database DATABASE
  1030. Specifies the database to query for the user. Defaults to ``default``.
  1031. Example usage::
  1032. django-admin changepassword ringo
  1033. ``createsuperuser``
  1034. ~~~~~~~~~~~~~~~~~~~
  1035. .. django-admin:: createsuperuser
  1036. This command is only available if Django's :doc:`authentication system
  1037. </topics/auth/index>` (``django.contrib.auth``) is installed.
  1038. Creates a superuser account (a user who has all permissions). This is
  1039. useful if you need to create an initial superuser account or if you need to
  1040. programmatically generate superuser accounts for your site(s).
  1041. When run interactively, this command will prompt for a password for
  1042. the new superuser account. When run non-interactively, you can provide
  1043. a password by setting the ``DJANGO_SUPERUSER_PASSWORD`` environment variable.
  1044. Otherwise, no password will be set, and the superuser account will not be able
  1045. to log in until a password has been manually set for it.
  1046. In non-interactive mode, the
  1047. :attr:`~django.contrib.auth.models.CustomUser.USERNAME_FIELD` and required
  1048. fields (listed in
  1049. :attr:`~django.contrib.auth.models.CustomUser.REQUIRED_FIELDS`) fall back to
  1050. ``DJANGO_SUPERUSER_<uppercase_field_name>`` environment variables, unless they
  1051. are overridden by a command line argument. For example, to provide an ``email``
  1052. field, you can use ``DJANGO_SUPERUSER_EMAIL`` environment variable.
  1053. .. versionchanged:: 3.0
  1054. Support for using ``DJANGO_SUPERUSER_PASSWORD`` and
  1055. ``DJANGO_SUPERUSER_<uppercase_field_name>`` environment variables was added.
  1056. .. django-admin-option:: --noinput, --no-input
  1057. Suppresses all user prompts. If a suppressed prompt cannot be resolved
  1058. automatically, the command will exit with error code 1.
  1059. .. django-admin-option:: --username USERNAME
  1060. .. django-admin-option:: --email EMAIL
  1061. The username and email address for the new account can be supplied by
  1062. using the ``--username`` and ``--email`` arguments on the command
  1063. line. If either of those is not supplied, ``createsuperuser`` will prompt for
  1064. it when running interactively.
  1065. .. django-admin-option:: --database DATABASE
  1066. Specifies the database into which the superuser object will be saved.
  1067. You can subclass the management command and override ``get_input_data()`` if you
  1068. want to customize data input and validation. Consult the source code for
  1069. details on the existing implementation and the method's parameters. For example,
  1070. it could be useful if you have a ``ForeignKey`` in
  1071. :attr:`~django.contrib.auth.models.CustomUser.REQUIRED_FIELDS` and want to
  1072. allow creating an instance instead of entering the primary key of an existing
  1073. instance.
  1074. ``django.contrib.contenttypes``
  1075. -------------------------------
  1076. ``remove_stale_contenttypes``
  1077. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1078. .. django-admin:: remove_stale_contenttypes
  1079. This command is only available if Django's :doc:`contenttypes app
  1080. </ref/contrib/contenttypes>` (:mod:`django.contrib.contenttypes`) is installed.
  1081. Deletes stale content types (from deleted models) in your database. Any objects
  1082. that depend on the deleted content types will also be deleted. A list of
  1083. deleted objects will be displayed before you confirm it's okay to proceed with
  1084. the deletion.
  1085. .. django-admin-option:: --database DATABASE
  1086. Specifies the database to use. Defaults to ``default``.
  1087. ``django.contrib.gis``
  1088. ----------------------
  1089. ``ogrinspect``
  1090. ~~~~~~~~~~~~~~
  1091. This command is only available if :doc:`GeoDjango </ref/contrib/gis/index>`
  1092. (``django.contrib.gis``) is installed.
  1093. Please refer to its :djadmin:`description <ogrinspect>` in the GeoDjango
  1094. documentation.
  1095. ``django.contrib.sessions``
  1096. ---------------------------
  1097. ``clearsessions``
  1098. ~~~~~~~~~~~~~~~~~
  1099. .. django-admin:: clearsessions
  1100. Can be run as a cron job or directly to clean out expired sessions.
  1101. ``django.contrib.sitemaps``
  1102. ---------------------------
  1103. ``ping_google``
  1104. ~~~~~~~~~~~~~~~
  1105. This command is only available if the :doc:`Sitemaps framework
  1106. </ref/contrib/sitemaps>` (``django.contrib.sitemaps``) is installed.
  1107. Please refer to its :djadmin:`description <ping_google>` in the Sitemaps
  1108. documentation.
  1109. ``django.contrib.staticfiles``
  1110. ------------------------------
  1111. ``collectstatic``
  1112. ~~~~~~~~~~~~~~~~~
  1113. This command is only available if the :doc:`static files application
  1114. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
  1115. Please refer to its :djadmin:`description <collectstatic>` in the
  1116. :doc:`staticfiles </ref/contrib/staticfiles>` documentation.
  1117. ``findstatic``
  1118. ~~~~~~~~~~~~~~
  1119. This command is only available if the :doc:`static files application
  1120. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
  1121. Please refer to its :djadmin:`description <findstatic>` in the :doc:`staticfiles
  1122. </ref/contrib/staticfiles>` documentation.
  1123. Default options
  1124. ===============
  1125. .. program:: None
  1126. Although some commands may allow their own custom options, every command
  1127. allows for the following options:
  1128. .. django-admin-option:: --pythonpath PYTHONPATH
  1129. Adds the given filesystem path to the Python `import search path`_. If this
  1130. isn't provided, ``django-admin`` will use the ``PYTHONPATH`` environment
  1131. variable.
  1132. This option is unnecessary in ``manage.py``, because it takes care of setting
  1133. the Python path for you.
  1134. Example usage::
  1135. django-admin migrate --pythonpath='/home/djangoprojects/myproject'
  1136. .. _import search path: https://www.diveinto.org/python3/your-first-python-program.html#importsearchpath
  1137. .. django-admin-option:: --settings SETTINGS
  1138. Specifies the settings module to use. The settings module should be in Python
  1139. package syntax, e.g. ``mysite.settings``. If this isn't provided,
  1140. ``django-admin`` will use the ``DJANGO_SETTINGS_MODULE`` environment variable.
  1141. This option is unnecessary in ``manage.py``, because it uses
  1142. ``settings.py`` from the current project by default.
  1143. Example usage::
  1144. django-admin migrate --settings=mysite.settings
  1145. .. django-admin-option:: --traceback
  1146. Displays a full stack trace when a :exc:`~django.core.management.CommandError`
  1147. is raised. By default, ``django-admin`` will show an error message when a
  1148. ``CommandError`` occurs and a full stack trace for any other exception.
  1149. Example usage::
  1150. django-admin migrate --traceback
  1151. .. django-admin-option:: --verbosity {0,1,2,3}, -v {0,1,2,3}
  1152. Specifies the amount of notification and debug information that a command
  1153. should print to the console.
  1154. * ``0`` means no output.
  1155. * ``1`` means normal output (default).
  1156. * ``2`` means verbose output.
  1157. * ``3`` means *very* verbose output.
  1158. Example usage::
  1159. django-admin migrate --verbosity 2
  1160. .. django-admin-option:: --no-color
  1161. Disables colorized command output. Some commands format their output to be
  1162. colorized. For example, errors will be printed to the console in red and SQL
  1163. statements will be syntax highlighted.
  1164. Example usage::
  1165. django-admin runserver --no-color
  1166. .. django-admin-option:: --force-color
  1167. Forces colorization of the command output if it would otherwise be disabled
  1168. as discussed in :ref:`syntax-coloring`. For example, you may want to pipe
  1169. colored output to another command.
  1170. .. django-admin-option:: --skip-checks
  1171. .. versionadded:: 3.0
  1172. Skips running system checks prior to running the command. This option is only
  1173. available if the
  1174. :attr:`~django.core.management.BaseCommand.requires_system_checks` command
  1175. attribute is set to ``True``.
  1176. Example usage::
  1177. django-admin migrate --skip-checks
  1178. Extra niceties
  1179. ==============
  1180. .. _syntax-coloring:
  1181. Syntax coloring
  1182. ---------------
  1183. The ``django-admin`` / ``manage.py`` commands will use pretty
  1184. color-coded output if your terminal supports ANSI-colored output. It
  1185. won't use the color codes if you're piping the command's output to
  1186. another program unless the :option:`--force-color` option is used.
  1187. Under Windows, the native console doesn't support ANSI escape sequences so by
  1188. default there is no color output. But you can install the `ANSICON`_
  1189. third-party tool, the Django commands will detect its presence and will make
  1190. use of its services to color output just like on Unix-based platforms.
  1191. The colors used for syntax highlighting can be customized. Django
  1192. ships with three color palettes:
  1193. * ``dark``, suited to terminals that show white text on a black
  1194. background. This is the default palette.
  1195. * ``light``, suited to terminals that show black text on a white
  1196. background.
  1197. * ``nocolor``, which disables syntax highlighting.
  1198. You select a palette by setting a ``DJANGO_COLORS`` environment
  1199. variable to specify the palette you want to use. For example, to
  1200. specify the ``light`` palette under a Unix or OS/X BASH shell, you
  1201. would run the following at a command prompt::
  1202. export DJANGO_COLORS="light"
  1203. You can also customize the colors that are used. Django specifies a
  1204. number of roles in which color is used:
  1205. * ``error`` - A major error.
  1206. * ``notice`` - A minor error.
  1207. * ``success`` - A success.
  1208. * ``warning`` - A warning.
  1209. * ``sql_field`` - The name of a model field in SQL.
  1210. * ``sql_coltype`` - The type of a model field in SQL.
  1211. * ``sql_keyword`` - An SQL keyword.
  1212. * ``sql_table`` - The name of a model in SQL.
  1213. * ``http_info`` - A 1XX HTTP Informational server response.
  1214. * ``http_success`` - A 2XX HTTP Success server response.
  1215. * ``http_not_modified`` - A 304 HTTP Not Modified server response.
  1216. * ``http_redirect`` - A 3XX HTTP Redirect server response other than 304.
  1217. * ``http_not_found`` - A 404 HTTP Not Found server response.
  1218. * ``http_bad_request`` - A 4XX HTTP Bad Request server response other than 404.
  1219. * ``http_server_error`` - A 5XX HTTP Server Error response.
  1220. * ``migrate_heading`` - A heading in a migrations management command.
  1221. * ``migrate_label`` - A migration name.
  1222. Each of these roles can be assigned a specific foreground and
  1223. background color, from the following list:
  1224. * ``black``
  1225. * ``red``
  1226. * ``green``
  1227. * ``yellow``
  1228. * ``blue``
  1229. * ``magenta``
  1230. * ``cyan``
  1231. * ``white``
  1232. Each of these colors can then be modified by using the following
  1233. display options:
  1234. * ``bold``
  1235. * ``underscore``
  1236. * ``blink``
  1237. * ``reverse``
  1238. * ``conceal``
  1239. A color specification follows one of the following patterns:
  1240. * ``role=fg``
  1241. * ``role=fg/bg``
  1242. * ``role=fg,option,option``
  1243. * ``role=fg/bg,option,option``
  1244. where ``role`` is the name of a valid color role, ``fg`` is the
  1245. foreground color, ``bg`` is the background color and each ``option``
  1246. is one of the color modifying options. Multiple color specifications
  1247. are then separated by a semicolon. For example::
  1248. export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"
  1249. would specify that errors be displayed using blinking yellow on blue,
  1250. and notices displayed using magenta. All other color roles would be
  1251. left uncolored.
  1252. Colors can also be specified by extending a base palette. If you put
  1253. a palette name in a color specification, all the colors implied by that
  1254. palette will be loaded. So::
  1255. export DJANGO_COLORS="light;error=yellow/blue,blink;notice=magenta"
  1256. would specify the use of all the colors in the light color palette,
  1257. *except* for the colors for errors and notices which would be
  1258. overridden as specified.
  1259. .. _ANSICON: http://adoxa.altervista.org/ansicon/
  1260. Bash completion
  1261. ---------------
  1262. If you use the Bash shell, consider installing the Django bash completion
  1263. script, which lives in ``extras/django_bash_completion`` in the Django source
  1264. distribution. It enables tab-completion of ``django-admin`` and
  1265. ``manage.py`` commands, so you can, for instance...
  1266. * Type ``django-admin``.
  1267. * Press [TAB] to see all available options.
  1268. * Type ``sql``, then [TAB], to see all available options whose names start
  1269. with ``sql``.
  1270. See :doc:`/howto/custom-management-commands` for how to add customized actions.
  1271. ==========================================
  1272. Running management commands from your code
  1273. ==========================================
  1274. .. function:: django.core.management.call_command(name, *args, **options)
  1275. To call a management command from code use ``call_command``.
  1276. ``name``
  1277. the name of the command to call or a command object. Passing the name is
  1278. preferred unless the object is required for testing.
  1279. ``*args``
  1280. a list of arguments accepted by the command. Arguments are passed to the
  1281. argument parser, so you can use the same style as you would on the command
  1282. line. For example, ``call_command('flush', '--verbosity=0')``.
  1283. ``**options``
  1284. named options accepted on the command-line. Options are passed to the command
  1285. without triggering the argument parser, which means you'll need to pass the
  1286. correct type. For example, ``call_command('flush', verbosity=0)`` (zero must
  1287. be an integer rather than a string).
  1288. Examples::
  1289. from django.core import management
  1290. from django.core.management.commands import loaddata
  1291. management.call_command('flush', verbosity=0, interactive=False)
  1292. management.call_command('loaddata', 'test_data', verbosity=0)
  1293. management.call_command(loaddata.Command(), 'test_data', verbosity=0)
  1294. Note that command options that take no arguments are passed as keywords
  1295. with ``True`` or ``False``, as you can see with the ``interactive`` option above.
  1296. Named arguments can be passed by using either one of the following syntaxes::
  1297. # Similar to the command line
  1298. management.call_command('dumpdata', '--natural-foreign')
  1299. # Named argument similar to the command line minus the initial dashes and
  1300. # with internal dashes replaced by underscores
  1301. management.call_command('dumpdata', natural_foreign=True)
  1302. # `use_natural_foreign_keys` is the option destination variable
  1303. management.call_command('dumpdata', use_natural_foreign_keys=True)
  1304. Some command options have different names when using ``call_command()`` instead
  1305. of ``django-admin`` or ``manage.py``. For example, ``django-admin
  1306. createsuperuser --no-input`` translates to ``call_command('createsuperuser',
  1307. interactive=False)``. To find what keyword argument name to use for
  1308. ``call_command()``, check the command's source code for the ``dest`` argument
  1309. passed to ``parser.add_argument()``.
  1310. Command options which take multiple options are passed a list::
  1311. management.call_command('dumpdata', exclude=['contenttypes', 'auth'])
  1312. The return value of the ``call_command()`` function is the same as the return
  1313. value of the ``handle()`` method of the command.
  1314. Output redirection
  1315. ==================
  1316. Note that you can redirect standard output and error streams as all commands
  1317. support the ``stdout`` and ``stderr`` options. For example, you could write::
  1318. with open('/path/to/command_output', 'w') as f:
  1319. management.call_command('dumpdata', stdout=f)