django-admin.txt 73 KB

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