{"title":"Writing custom django-admin commands","version":"1.8","locale":"en","docname":"howto/custom-management-commands","url":"/en/1.8/howto/custom-management-commands/","canonical":"https://djangodocs.dev/en/1.8/howto/custom-management-commands/","summary":"Applications can register their own actions with manage.py . For example, you might want to add a manage.py action for a Django app that you’re distributing. In…","html":"<span id=\"writing-custom-django-admin-commands\"></span><h1>Writing custom django-admin commands<a class=\"heading-anchor\" href=\"#module-django.core.management\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Applications can register their own actions with <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>. For example,\nyou might want to add a <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> action for a Django app that you’re\ndistributing. In this document, we will be building a custom <code class=\"docutils literal notranslate\"><span class=\"pre\">closepoll</span></code>\ncommand for the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> application from the\n<a class=\"reference internal\" href=\"/en/1.8/intro/tutorial01/\"><span class=\"doc\">tutorial</span></a>.</p>\n<p>To do this, just add a <code class=\"docutils literal notranslate\"><span class=\"pre\">management/commands</span></code> directory to the application.\nDjango will register a <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> command for each Python module in that\ndirectory whose name doesn’t begin with an underscore. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">polls</span><span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">management</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">commands</span><span class=\"o\">/</span>\n            <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n            <span class=\"n\">_private</span><span class=\"o\">.</span><span class=\"n\">py</span>\n            <span class=\"n\">closepoll</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">py</span>\n</code></pre></div>\n<p>On Python 2, be sure to include <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__.py</span></code> files in both the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">management</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">management/commands</span></code> directories as done above or your\ncommand will not be detected.</p>\n<p>In this example, the <code class=\"docutils literal notranslate\"><span class=\"pre\">closepoll</span></code> command will be made available to any project\nthat includes the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> application in <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">_private.py</span></code> module will not be available as a management command.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">closepoll.py</span></code> module has only one requirement – it must define a class\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Command</span></code> that extends <a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a> or one of its\n<a class=\"reference internal\" href=\"#ref-basecommand-subclasses\"><span class=\"std std-ref\">subclasses</span></a>.</p>\n<aside class=\"admonition-standalone-scripts admonition\">\n<p class=\"admonition-title\">Standalone scripts</p>\n<p>Custom management commands are especially useful for running standalone\nscripts or for scripts that are periodically executed from the UNIX crontab\nor from Windows scheduled tasks control panel.</p>\n</aside>\n<p>To implement the command, edit <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/management/commands/closepoll.py</span></code> to\nlook like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.management.base</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">BaseCommand</span><span class=\"p\">,</span> <span class=\"n\">CommandError</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">polls.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Poll</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Command</span><span class=\"p\">(</span><span class=\"n\">BaseCommand</span><span class=\"p\">):</span>\n    <span class=\"n\">help</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;Closes the specified poll for voting&#39;</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">add_arguments</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">parser</span><span class=\"p\">):</span>\n        <span class=\"n\">parser</span><span class=\"o\">.</span><span class=\"n\">add_argument</span><span class=\"p\">(</span><span class=\"s1\">&#39;poll_id&#39;</span><span class=\"p\">,</span> <span class=\"n\">nargs</span><span class=\"o\">=</span><span class=\"s1\">&#39;+&#39;</span><span class=\"p\">,</span> <span class=\"nb\">type</span><span class=\"o\">=</span><span class=\"nb\">int</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">handle</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">):</span>\n        <span class=\"k\">for</span> <span class=\"n\">poll_id</span> <span class=\"ow\">in</span> <span class=\"n\">options</span><span class=\"p\">[</span><span class=\"s1\">&#39;poll_id&#39;</span><span class=\"p\">]:</span>\n            <span class=\"k\">try</span><span class=\"p\">:</span>\n                <span class=\"n\">poll</span> <span class=\"o\">=</span> <span class=\"n\">Poll</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">pk</span><span class=\"o\">=</span><span class=\"n\">poll_id</span><span class=\"p\">)</span>\n            <span class=\"k\">except</span> <span class=\"n\">Poll</span><span class=\"o\">.</span><span class=\"n\">DoesNotExist</span><span class=\"p\">:</span>\n                <span class=\"k\">raise</span> <span class=\"n\">CommandError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Poll &quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot; does not exist&#39;</span> <span class=\"o\">%</span> <span class=\"n\">poll_id</span><span class=\"p\">)</span>\n\n            <span class=\"n\">poll</span><span class=\"o\">.</span><span class=\"n\">opened</span> <span class=\"o\">=</span> <span class=\"kc\">False</span>\n            <span class=\"n\">poll</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">stdout</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"s1\">&#39;Successfully closed poll &quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot;&#39;</span> <span class=\"o\">%</span> <span class=\"n\">poll_id</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>Before Django 1.8, management commands were based on the <a class=\"reference external\" href=\"https://docs.python.org/3/library/optparse.html#module-optparse\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">optparse</span></code></a>\nmodule, and positional arguments were passed in <code class=\"docutils literal notranslate\"><span class=\"pre\">*args</span></code> while optional\narguments were passed in <code class=\"docutils literal notranslate\"><span class=\"pre\">**options</span></code>. Now that management commands use\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#module-argparse\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">argparse</span></code></a> for argument parsing, all arguments are passed in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">**options</span></code> by default, unless you name your positional arguments to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">args</span></code> (compatibility mode). You are encouraged to exclusively use\n<code class=\"docutils literal notranslate\"><span class=\"pre\">**options</span></code> for new commands.</p>\n</aside>\n<aside class=\"admonition admonition-note\" id=\"management-commands-output\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>When you are using management commands and wish to provide console\noutput, you should write to <code class=\"docutils literal notranslate\"><span class=\"pre\">self.stdout</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">self.stderr</span></code>,\ninstead of printing to <code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">stderr</span></code> directly. By\nusing these proxies, it becomes much easier to test your custom\ncommand. Note also that you don’t need to end messages with a newline\ncharacter, it will be added automatically, unless you specify the <code class=\"docutils literal notranslate\"><span class=\"pre\">ending</span></code>\nparameter:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">stdout</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"s2\">&quot;Unterminated line&quot;</span><span class=\"p\">,</span> <span class=\"n\">ending</span><span class=\"o\">=</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</aside>\n<p>The new custom command can be called using <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">closepoll</span>\n<span class=\"pre\">&lt;poll_id&gt;</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">handle()</span></code> method takes one or more <code class=\"docutils literal notranslate\"><span class=\"pre\">poll_ids</span></code> and sets <code class=\"docutils literal notranslate\"><span class=\"pre\">poll.opened</span></code>\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> for each one. If the user referenced any nonexistent polls, a\n<a class=\"reference internal\" href=\"#django.core.management.CommandError\" title=\"django.core.management.CommandError\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a> is raised. The <code class=\"docutils literal notranslate\"><span class=\"pre\">poll.opened</span></code> attribute does not exist\nin the <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial01/\"><span class=\"doc\">tutorial</span></a> and was added to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls.models.Poll</span></code> for this example.</p>\n<section id=\"accepting-optional-arguments\">\n<span id=\"custom-commands-options\"></span><h2>Accepting optional arguments<a class=\"heading-anchor\" href=\"#accepting-optional-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The same <code class=\"docutils literal notranslate\"><span class=\"pre\">closepoll</span></code> could be easily modified to delete a given poll instead\nof closing it by accepting additional command line options. These custom\noptions can be added in the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.add_arguments\" title=\"django.core.management.BaseCommand.add_arguments\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">add_arguments()</span></code></a> method like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Command</span><span class=\"p\">(</span><span class=\"n\">BaseCommand</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">add_arguments</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">parser</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Positional arguments</span>\n        <span class=\"n\">parser</span><span class=\"o\">.</span><span class=\"n\">add_argument</span><span class=\"p\">(</span><span class=\"s1\">&#39;poll_id&#39;</span><span class=\"p\">,</span> <span class=\"n\">nargs</span><span class=\"o\">=</span><span class=\"s1\">&#39;+&#39;</span><span class=\"p\">,</span> <span class=\"nb\">type</span><span class=\"o\">=</span><span class=\"nb\">int</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Named (optional) arguments</span>\n        <span class=\"n\">parser</span><span class=\"o\">.</span><span class=\"n\">add_argument</span><span class=\"p\">(</span><span class=\"s1\">&#39;--delete&#39;</span><span class=\"p\">,</span>\n            <span class=\"n\">action</span><span class=\"o\">=</span><span class=\"s1\">&#39;store_true&#39;</span><span class=\"p\">,</span>\n            <span class=\"n\">dest</span><span class=\"o\">=</span><span class=\"s1\">&#39;delete&#39;</span><span class=\"p\">,</span>\n            <span class=\"n\">default</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span>\n            <span class=\"n\">help</span><span class=\"o\">=</span><span class=\"s1\">&#39;Delete poll instead of closing it&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">handle</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">):</span>\n        <span class=\"c1\"># ...</span>\n        <span class=\"k\">if</span> <span class=\"n\">options</span><span class=\"p\">[</span><span class=\"s1\">&#39;delete&#39;</span><span class=\"p\">]:</span>\n            <span class=\"n\">poll</span><span class=\"o\">.</span><span class=\"n\">delete</span><span class=\"p\">()</span>\n        <span class=\"c1\"># ...</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>Previously, only the standard <a class=\"reference external\" href=\"https://docs.python.org/3/library/optparse.html#module-optparse\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">optparse</span></code></a> library was supported and\nyou would have to extend the command <code class=\"docutils literal notranslate\"><span class=\"pre\">option_list</span></code> variable with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">optparse.make_option()</span></code>.</p>\n</aside>\n<p>The option (<code class=\"docutils literal notranslate\"><span class=\"pre\">delete</span></code> in our example) is available in the options dict\nparameter of the handle method. See the <a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#module-argparse\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">argparse</span></code></a> Python documentation\nfor more about <code class=\"docutils literal notranslate\"><span class=\"pre\">add_argument</span></code> usage.</p>\n<p>In addition to being able to add custom command line options, all\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">management commands</span></a> can accept some\ndefault options such as <code class=\"docutils literal notranslate\"><span class=\"pre\">--verbosity</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">--traceback</span></code>.</p>\n</section>\n<section id=\"management-commands-and-locales\">\n<span id=\"id1\"></span><h2>Management commands and locales<a class=\"heading-anchor\" href=\"#management-commands-and-locales\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>By default, the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.execute\" title=\"django.core.management.BaseCommand.execute\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">BaseCommand.execute()</span></code></a> method deactivates translations\nbecause some commands shipped with Django perform several tasks (for example,\nuser-facing content rendering and database population) that require a\nproject-neutral string language.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>In previous versions, Django forced the “en-us” locale instead of\ndeactivating translations.</p>\n</aside>\n<p>If, for some reason, your custom management command needs to use a fixed locale,\nyou should manually activate and deactivate it in your\n<a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a> method using the functions provided by the I18N\nsupport code:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.management.base</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">BaseCommand</span><span class=\"p\">,</span> <span class=\"n\">CommandError</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">translation</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Command</span><span class=\"p\">(</span><span class=\"n\">BaseCommand</span><span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    <span class=\"n\">can_import_settings</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">handle</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">):</span>\n\n        <span class=\"c1\"># Activate a fixed locale, e.g. Russian</span>\n        <span class=\"n\">translation</span><span class=\"o\">.</span><span class=\"n\">activate</span><span class=\"p\">(</span><span class=\"s1\">&#39;ru&#39;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Or you can activate the LANGUAGE_CODE # chosen in the settings:</span>\n        <span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n        <span class=\"n\">translation</span><span class=\"o\">.</span><span class=\"n\">activate</span><span class=\"p\">(</span><span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">LANGUAGE_CODE</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># Your command logic here</span>\n        <span class=\"o\">...</span>\n\n        <span class=\"n\">translation</span><span class=\"o\">.</span><span class=\"n\">deactivate</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Another need might be that your command simply should use the locale set in\nsettings and Django should be kept from deactivating it. You can achieve\nit by using the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.leave_locale_alone\" title=\"django.core.management.BaseCommand.leave_locale_alone\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">BaseCommand.leave_locale_alone</span></code></a> option.</p>\n<p>When working on the scenarios described above though, take into account that\nsystem management commands typically have to be very careful about running in\nnon-uniform locales, so you might need to:</p>\n<ul class=\"simple\">\n<li><p>Make sure the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-USE_I18N\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_I18N</span></code></a> setting is always <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> when running\nthe command (this is a good example of the potential problems stemming\nfrom a dynamic runtime environment that Django commands avoid offhand by\ndeactivating translations).</p></li>\n<li><p>Review the code of your command and the code it calls for behavioral\ndifferences when locales are changed and evaluate its impact on\npredictable behavior of your command.</p></li>\n</ul>\n</section>\n<section id=\"testing\">\n<h2>Testing<a class=\"heading-anchor\" href=\"#testing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Information on how to test custom management commands can be found in the\n<a class=\"reference internal\" href=\"/en/1.8/topics/testing/tools/#topics-testing-management-commands\"><span class=\"std std-ref\">testing docs</span></a>.</p>\n</section>\n<section id=\"command-objects\">\n<h2>Command objects<a class=\"heading-anchor\" href=\"#command-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BaseCommand</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The base class from which all management commands ultimately derive.</p>\n<p>Use this class if you want access to all of the mechanisms which\nparse the command-line arguments and work out what code to call in\nresponse; if you don’t need to change any of that behavior,\nconsider using one of its <a class=\"reference internal\" href=\"#ref-basecommand-subclasses\"><span class=\"std std-ref\">subclasses</span></a>.</p>\n<p>Subclassing the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a> class requires that you implement the\n<a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a> method.</p>\n<section id=\"attributes\">\n<h3>Attributes<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>All attributes can be set in your derived class and can be used in\n<a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a>’s <a class=\"reference internal\" href=\"#ref-basecommand-subclasses\"><span class=\"std std-ref\">subclasses</span></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.args\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">args</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.args\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string listing the arguments accepted by the command,\nsuitable for use in help messages; e.g., a command which takes\na list of application names might set this to ‘&lt;app_label\napp_label …&gt;’.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>This should be done now in the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.add_arguments\" title=\"django.core.management.BaseCommand.add_arguments\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">add_arguments()</span></code></a>\nmethod, by calling the <code class=\"docutils literal notranslate\"><span class=\"pre\">parser.add_argument()</span></code> method. See the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">closepoll</span></code> example above.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.can_import_settings\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">can_import_settings</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.can_import_settings\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean indicating whether the command needs to be able to\nimport Django settings; if <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">execute()</span></code> will verify\nthat this is possible before proceeding. Default value is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.help\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">help</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.help\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A short description of the command, which will be printed in the\nhelp message when the user runs the command\n<code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">help</span> <span class=\"pre\">&lt;command&gt;</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.missing_args_message\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">missing_args_message</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.missing_args_message\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.8\">\n<p class=\"version-note-title\">New in Django 1.8</p></aside>\n<p>If your command defines mandatory positional arguments, you can customize\nthe message error returned in the case of missing arguments. The default is\noutput by <a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#module-argparse\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">argparse</span></code></a> (“too few arguments”).</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.option_list\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">option_list</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.option_list\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This is the list of <code class=\"docutils literal notranslate\"><span class=\"pre\">optparse</span></code> options which will be fed\ninto the command’s <code class=\"docutils literal notranslate\"><span class=\"pre\">OptionParser</span></code> for parsing arguments.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>You should now override the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.add_arguments\" title=\"django.core.management.BaseCommand.add_arguments\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">add_arguments()</span></code></a> method\nto add custom arguments accepted by your command. See <a class=\"reference internal\" href=\"#custom-commands-options\"><span class=\"std std-ref\">the example\nabove</span></a>.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.output_transaction\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">output_transaction</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.output_transaction\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean indicating whether the command outputs SQL statements; if\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, the output will automatically be wrapped with <code class=\"docutils literal notranslate\"><span class=\"pre\">BEGIN;</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">COMMIT;</span></code>. Default value is <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.requires_system_checks\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">requires_system_checks</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.requires_system_checks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.7\">\n<p class=\"version-note-title\">New in Django 1.7</p></aside>\n<p>A boolean; if <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, the entire Django project will be checked for\npotential problems prior to executing the command. If\n<code class=\"docutils literal notranslate\"><span class=\"pre\">requires_system_checks</span></code> is missing, the value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">requires_model_validation</span></code> is used. If the latter flag is missing\nas well, the default value (<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>) is used. Defining both\n<code class=\"docutils literal notranslate\"><span class=\"pre\">requires_system_checks</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">requires_model_validation</span></code> will result\nin an error.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.requires_model_validation\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">requires_model_validation</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.requires_model_validation\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-deprecated\" data-version=\"1.7\">\n<p class=\"version-note-title\">Deprecated since Django 1.7</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.7: </span>Replaced by <code class=\"docutils literal notranslate\"><span class=\"pre\">requires_system_checks</span></code></p>\n</aside>\n<p>A boolean; if <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, validation of installed models will be performed\nprior to executing the command. Default value is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. To validate an\nindividual application’s models rather than all applications’ models, call\n<a class=\"reference internal\" href=\"#django.core.management.BaseCommand.validate\" title=\"django.core.management.BaseCommand.validate\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">validate()</span></code></a> from <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.leave_locale_alone\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">leave_locale_alone</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.leave_locale_alone\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean indicating whether the locale set in settings should be preserved\nduring the execution of the command instead of being forcibly set to ‘en-us’.</p>\n<p>Default value is <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p>\n<p>Make sure you know what you are doing if you decide to change the value of\nthis option in your custom command if it creates database content that\nis locale-sensitive and such content shouldn’t contain any translations\n(like it happens e.g. with django.contrib.auth permissions) as making the\nlocale differ from the de facto default ‘en-us’ might cause unintended\neffects. Seethe <a class=\"reference internal\" href=\"#id1\">Management commands and locales</a> section above for\nfurther details.</p>\n<p>This option can’t be <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> when the\n<a class=\"reference internal\" href=\"#django.core.management.BaseCommand.can_import_settings\" title=\"django.core.management.BaseCommand.can_import_settings\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">can_import_settings</span></code></a> option is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> too\nbecause attempting to set the locale needs access to settings. This\ncondition will generate a <a class=\"reference internal\" href=\"#django.core.management.CommandError\" title=\"django.core.management.CommandError\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.style\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">style</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.style\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>An instance attribute that helps create colored output when writing to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">stderr</span></code>. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">stdout</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">style</span><span class=\"o\">.</span><span class=\"n\">NOTICE</span><span class=\"p\">(</span><span class=\"s1\">&#39;...&#39;</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#syntax-coloring\"><span class=\"std std-ref\">Syntax coloring</span></a> to learn how to modify the color palette and to\nsee the available styles (use uppercased versions of the “roles” described\nin that section).</p>\n<p>If you pass the <code class=\"docutils literal notranslate\"><span class=\"pre\">--no-color</span></code> option when running your\ncommand, all <code class=\"docutils literal notranslate\"><span class=\"pre\">self.style()</span></code> calls will return the original string\nuncolored.</p>\n</dd></dl>\n\n</section>\n<section id=\"methods\">\n<h3>Methods<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a> has a few methods that can be overridden but only\nthe <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a> method must be implemented.</p>\n<aside class=\"admonition-implementing-a-constructor-in-a-subclass admonition\">\n<p class=\"admonition-title\">Implementing a constructor in a subclass</p>\n<p>If you implement <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> in your subclass of <a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a>,\nyou must call <a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a>’s <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Command</span><span class=\"p\">(</span><span class=\"n\">BaseCommand</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n        <span class=\"nb\">super</span><span class=\"p\">(</span><span class=\"n\">Command</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n        <span class=\"c1\"># ...</span>\n</code></pre></div>\n</aside>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.add_arguments\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">add_arguments</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">parser</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.add_arguments\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.8\">\n<p class=\"version-note-title\">New in Django 1.8</p></aside>\n<p>Entry point to add parser arguments to handle command line arguments passed\nto the command. Custom commands should override this method to add both\npositional and optional arguments accepted by the command. Calling\n<code class=\"docutils literal notranslate\"><span class=\"pre\">super()</span></code> is not needed when directly subclassing <code class=\"docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.get_version\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_version</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.get_version\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the Django version, which should be correct for all built-in Django\ncommands. User-supplied commands can override this method to return their\nown version.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.execute\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">execute</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">*</span></span><span class=\"n\"><span class=\"pre\">args</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.execute\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Tries to execute this command, performing system checks if needed (as\ncontrolled by the <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.requires_system_checks\" title=\"django.core.management.BaseCommand.requires_system_checks\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">requires_system_checks</span></code></a> attribute). If the command\nraises a <a class=\"reference internal\" href=\"#django.core.management.CommandError\" title=\"django.core.management.CommandError\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a>, it’s intercepted and printed to stderr.</p>\n</dd></dl>\n\n<aside class=\"admonition-calling-a-management-command-in-your-code admonition\">\n<p class=\"admonition-title\">Calling a management command in your code</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">execute()</span></code> should not be called directly from your code to execute a\ncommand. Use <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#call-command\"><span class=\"std std-ref\">call_command</span></a> instead.</p>\n</aside>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.handle\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">handle</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">*</span></span><span class=\"n\"><span class=\"pre\">args</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.handle\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The actual logic of the command. Subclasses must implement this method.</p>\n<p>It may return a Unicode string which will be printed to <code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code> (wrapped\nby <code class=\"docutils literal notranslate\"><span class=\"pre\">BEGIN;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">COMMIT;</span></code> if <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.output_transaction\" title=\"django.core.management.BaseCommand.output_transaction\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">output_transaction</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>).</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.check\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">check</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">app_configs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">tags</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">display_num_errors</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.check\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"1.7\">\n<p class=\"version-note-title\">New in Django 1.7</p></aside>\n<p>Uses the system check framework to inspect the entire Django project for\npotential problems. Serious problems are raised as a <a class=\"reference internal\" href=\"#django.core.management.CommandError\" title=\"django.core.management.CommandError\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a>;\nwarnings are output to stderr; minor notifications are output to stdout.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">app_configs</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">tags</span></code> are both <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, all system checks are\nperformed. <code class=\"docutils literal notranslate\"><span class=\"pre\">tags</span></code> can be a list of check tags, like <code class=\"docutils literal notranslate\"><span class=\"pre\">compatibility</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">models</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.validate\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">validate</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">app</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">display_num_errors</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.validate\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-deprecated\" data-version=\"1.7\">\n<p class=\"version-note-title\">Deprecated since Django 1.7</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.7: </span>Replaced with the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-check\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">check</span></code></a> command</p>\n</aside>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">app</span></code> is None, then all installed apps are checked for errors.</p>\n</dd></dl>\n\n</section>\n<section id=\"basecommand-subclasses\">\n<span id=\"ref-basecommand-subclasses\"></span><h3>BaseCommand subclasses<a class=\"heading-anchor\" href=\"#basecommand-subclasses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.AppCommand\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">AppCommand</span></span><a class=\"heading-anchor\" href=\"#django.core.management.AppCommand\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A management command which takes one or more installed application labels as\narguments, and does something with each of them.</p>\n<p>Rather than implementing <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a>, subclasses must\nimplement <a class=\"reference internal\" href=\"#django.core.management.AppCommand.handle_app_config\" title=\"django.core.management.AppCommand.handle_app_config\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle_app_config()</span></code></a>, which will be called once for\neach application.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.AppCommand.handle_app_config\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">AppCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">handle_app_config</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">app_config</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.AppCommand.handle_app_config\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Perform the command’s actions for <code class=\"docutils literal notranslate\"><span class=\"pre\">app_config</span></code>, which will be an\n<a class=\"reference internal\" href=\"/en/1.8/ref/applications/#django.apps.AppConfig\" title=\"django.apps.AppConfig\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AppConfig</span></code></a> instance corresponding to an application\nlabel given on the command line.</p>\n</dd></dl>\n\n<aside class=\"version-note version-changed\" data-version=\"1.7\">\n<p class=\"version-note-title\">Changed in Django 1.7</p><p>Previously, <a class=\"reference internal\" href=\"#django.core.management.AppCommand\" title=\"django.core.management.AppCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AppCommand</span></code></a> subclasses had to implement\n<code class=\"docutils literal notranslate\"><span class=\"pre\">handle_app(app,</span> <span class=\"pre\">**options)</span></code> where <code class=\"docutils literal notranslate\"><span class=\"pre\">app</span></code> was a models module. The new\nAPI makes it possible to handle applications without a models module. The\nfastest way to migrate is as follows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">handle_app_config</span><span class=\"p\">(</span><span class=\"n\">app_config</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">):</span>\n    <span class=\"k\">if</span> <span class=\"n\">app_config</span><span class=\"o\">.</span><span class=\"n\">models_module</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n        <span class=\"k\">return</span>                                  <span class=\"c1\"># Or raise an exception.</span>\n    <span class=\"n\">app</span> <span class=\"o\">=</span> <span class=\"n\">app_config</span><span class=\"o\">.</span><span class=\"n\">models_module</span>\n    <span class=\"c1\"># Copy the implementation of handle_app(app_config, **options) here.</span>\n</code></pre></div>\n<p>However, you may be able to simplify the implementation by using directly\nthe attributes of <code class=\"docutils literal notranslate\"><span class=\"pre\">app_config</span></code>.</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.LabelCommand\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LabelCommand</span></span><a class=\"heading-anchor\" href=\"#django.core.management.LabelCommand\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A management command which takes one or more arbitrary arguments (labels) on\nthe command line, and does something with each of them.</p>\n<p>Rather than implementing <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a>, subclasses must implement\n<a class=\"reference internal\" href=\"#django.core.management.LabelCommand.handle_label\" title=\"django.core.management.LabelCommand.handle_label\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle_label()</span></code></a>, which will be called once for each label.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.LabelCommand.handle_label\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">LabelCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">handle_label</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">label</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.LabelCommand.handle_label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Perform the command’s actions for <code class=\"docutils literal notranslate\"><span class=\"pre\">label</span></code>, which will be the string as\ngiven on the command line.</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.NoArgsCommand\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">NoArgsCommand</span></span><a class=\"heading-anchor\" href=\"#django.core.management.NoArgsCommand\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>Use <a class=\"reference internal\" href=\"#django.core.management.BaseCommand\" title=\"django.core.management.BaseCommand\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code></a> instead, which takes no arguments by default.</p>\n</aside>\n<p>A command which takes no arguments on the command line.</p>\n<p>Rather than implementing <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a>, subclasses must implement\n<a class=\"reference internal\" href=\"#django.core.management.NoArgsCommand.handle_noargs\" title=\"django.core.management.NoArgsCommand.handle_noargs\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle_noargs()</span></code></a>; <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.handle\" title=\"django.core.management.BaseCommand.handle\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">handle()</span></code></a> itself is\noverridden to ensure no arguments are passed to the command.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.NoArgsCommand.handle_noargs\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">NoArgsCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">handle_noargs</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.NoArgsCommand.handle_noargs\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Perform this command’s actions</p>\n</dd></dl>\n\n</section>\n<section id=\"command-exceptions\">\n<span id=\"ref-command-exceptions\"></span><h3>Command exceptions<a class=\"heading-anchor\" href=\"#command-exceptions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.CommandError\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CommandError</span></span><a class=\"heading-anchor\" href=\"#django.core.management.CommandError\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Exception class indicating a problem while executing a management command.</p>\n<p>If this exception is raised during the execution of a management command from a\ncommand line console, it will be caught and turned into a nicely-printed error\nmessage to the appropriate output stream (i.e., stderr); as a result, raising\nthis exception (with a sensible description of the error) is the preferred way\nto indicate that something has gone wrong in the execution of a command.</p>\n<p>If a management command is called from code through <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#call-command\"><span class=\"std std-ref\">call_command</span></a>, it’s up to you to catch the exception when needed.</p>\n</section>\n</section>","rootId":"module-django.core.management","toc":[{"title":"Accepting optional arguments","anchor":"accepting-optional-arguments","children":[]},{"title":"Management commands and locales","anchor":"management-commands-and-locales","children":[]},{"title":"Testing","anchor":"testing","children":[]},{"title":"Command objects","anchor":"command-objects","children":[{"title":"Attributes","anchor":"attributes","children":[]},{"title":"Methods","anchor":"methods","children":[]},{"title":"BaseCommand subclasses","anchor":"basecommand-subclasses","children":[]},{"title":"Command exceptions","anchor":"command-exceptions","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/1.8/howto/"}],"prev":{"docname":"howto/auth-remote-user","title":"Authentication using REMOTE_USER","url":"/en/1.8/howto/auth-remote-user/"},"next":{"docname":"howto/custom-model-fields","title":"Writing custom model fields","url":"/en/1.8/howto/custom-model-fields/"},"formats":{"html":"/en/1.8/howto/custom-management-commands/","markdown":"/en/1.8/howto/custom-management-commands.md","json":"/en/1.8/howto/custom-management-commands.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/howto/custom-management-commands.txt","official":"https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en"]}