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