django-admin.txt 64 KB

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