{"title":"How to create custom django-admin commands","version":"5.2","locale":"es","docname":"howto/custom-management-commands","url":"/es/5.2/howto/custom-management-commands/","canonical":"https://djangodocs.dev/es/5.2/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=\"how-to-create-custom-django-admin-commands\"></span><h1>How to create custom <code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code> 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=\"/es/5.2/intro/tutorial01/\"><span class=\"doc\">tutorial</span></a>.</p>\n<p>To do this, add a <code class=\"docutils literal notranslate\"><span class=\"pre\">management/commands</span></code> directory to the application. Django\nwill register a <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> command for each Python module in that directory\nwhose name doesn’t begin with an underscore. For example:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>polls/\n    __init__.py\n    models.py\n    management/\n        __init__.py\n        commands/\n            __init__.py\n            _private.py\n            closepoll.py\n    tests.py\n    views.py\n</code></pre></div>\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=\"/es/5.2/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>El módulo <code class=\"docutils literal notranslate\"><span class=\"pre\">_private.py</span></code> no estará disponible como comando de administración.</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>Para implementar el comando, edite <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/management/commands/closepoll.py</span></code> para que se parezca a:</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\">Question</span> <span class=\"k\">as</span> <span class=\"n\">Poll</span>\n\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=\"s2\">&quot;Closes the specified poll for voting&quot;</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=\"s2\">&quot;poll_ids&quot;</span><span class=\"p\">,</span> <span class=\"n\">nargs</span><span class=\"o\">=</span><span class=\"s2\">&quot;+&quot;</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=\"s2\">&quot;poll_ids&quot;</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>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">style</span><span class=\"o\">.</span><span class=\"n\">SUCCESS</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            <span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" id=\"management-commands-output\" role=\"note\">\n<p class=\"admonition-title\">Nota</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=\"s2\">&quot;&quot;</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_ids&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-exc 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 in\nthe <a class=\"reference internal\" href=\"/es/5.2/intro/tutorial02/\"><span class=\"doc\">tutorial</span></a> and was added to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls.models.Question</span></code> for this example.</p>\n<section id=\"accepting-optional-arguments\">\n<span id=\"custom-commands-options\"></span><h2>Aceptar los argumentos opcionales<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=\"s2\">&quot;poll_ids&quot;</span><span class=\"p\">,</span> <span class=\"n\">nargs</span><span class=\"o\">=</span><span class=\"s2\">&quot;+&quot;</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>\n            <span class=\"s2\">&quot;--delete&quot;</span><span class=\"p\">,</span>\n            <span class=\"n\">action</span><span class=\"o\">=</span><span class=\"s2\">&quot;store_true&quot;</span><span class=\"p\">,</span>\n            <span class=\"n\">help</span><span class=\"o\">=</span><span class=\"s2\">&quot;Delete poll instead of closing it&quot;</span><span class=\"p\">,</span>\n        <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=\"s2\">&quot;delete&quot;</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<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=\"(en Python versión 3.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=\"/es/5.2/ref/django-admin/\"><span class=\"doc\">management commands</span></a> can accept some default options\nsuch as <a class=\"reference internal\" href=\"/es/5.2/ref/django-admin/#cmdoption-verbosity\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--verbosity</span></code></a> and <a class=\"reference internal\" href=\"/es/5.2/ref/django-admin/#cmdoption-traceback\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--traceback</span></code></a>.</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, management commands are executed with the current active locale.</p>\n<p>If, for some reason, your custom management command must run without an active\nlocale (for example, to prevent translated content from being inserted into\nthe database), deactivate translations using the <code class=\"docutils literal notranslate\"><span class=\"pre\">&#64;no_translations</span></code>\ndecorator on your <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<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\">no_translations</span>\n\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\n    <span class=\"nd\">@no_translations</span>\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> <span class=\"o\">...</span>\n</code></pre></div>\n<p>Since translation deactivation requires access to configured settings, the\ndecorator can’t be used for commands that work without configured settings.</p>\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=\"/es/5.2/topics/testing/tools/#topics-testing-management-commands\"><span class=\"std std-ref\">testing docs</span></a>.</p>\n</section>\n<section id=\"overriding-commands\">\n<span id=\"id2\"></span><h2>Sobreescribiendo comandos<a class=\"heading-anchor\" href=\"#overriding-commands\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django registers the built-in commands and then searches for commands in\n<a class=\"reference internal\" href=\"/es/5.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> in reverse. During the search, if a command name\nduplicates an already registered command, the newly discovered command\noverrides the first.</p>\n<p>In other words, to override a command, the new command must have the same name\nand its app must be before the overridden command’s app in\n<a class=\"reference internal\" href=\"/es/5.2/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>Management commands from third-party apps that have been unintentionally\noverridden can be made available under a new name by creating a new command in\none of your project’s apps (ordered before the third-party app in\n<a class=\"reference internal\" href=\"/es/5.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>) which imports the <code class=\"docutils literal notranslate\"><span class=\"pre\">Command</span></code> of the overridden\ncommand.</p>\n</section>\n<section id=\"command-objects\">\n<h2>Objetos de Comando<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>Atributos<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.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>Una breve descripción del comando, que será impresa en el mensaje de ayuda cuando el usuario ejecute  <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;comando&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><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=\"(en Python versión 3.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.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_migrations_checks\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">requires_migrations_checks</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.requires_migrations_checks\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean; if <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, the command prints a warning if the set of\nmigrations on disk don’t match the migrations in the database. A warning\ndoesn’t prevent the command from executing. 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><p>A list or tuple of tags, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">[Tags.staticfiles,</span> <span class=\"pre\">Tags.models]</span></code>. System\nchecks <a class=\"reference internal\" href=\"/es/5.2/topics/checks/#registering-labeling-checks\"><span class=\"std std-ref\">registered in the chosen tags</span></a>\nwill be checked for errors prior to executing the command. The value\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'__all__'</span></code> can be used to specify that all system checks should be\nperformed. Default value is <code class=\"docutils literal notranslate\"><span class=\"pre\">'__all__'</span></code>.</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\">SUCCESS</span><span class=\"p\">(</span><span class=\"s2\">&quot;...&quot;</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/es/5.2/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 <a class=\"reference internal\" href=\"/es/5.2/ref/django-admin/#cmdoption-no-color\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">--no-color</span></code></a> option when running your command, all\n<code class=\"docutils literal notranslate\"><span class=\"pre\">self.style()</span></code> calls will return the original string uncolored.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.suppressed_base_arguments\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">suppressed_base_arguments</span></span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.suppressed_base_arguments\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The default command options to suppress in the help output. This should be\na set of option names (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'--verbosity'</span></code>). The default values for the\nsuppressed options are still passed.</p>\n</dd></dl>\n\n</section>\n<section id=\"methods\">\n<h3>Métodos<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\">Implementando un constructor en una subclase</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=\"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.create_parser\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">create_parser</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">prog_name</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">subcommand</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.create_parser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a <code class=\"docutils literal notranslate\"><span class=\"pre\">CommandParser</span></code> instance, which is an\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser\" title=\"(en Python versión 3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ArgumentParser</span></code></a> subclass with a few customizations for\nDjango.</p>\n<p>You can customize the instance by overriding this method and calling\n<code class=\"docutils literal notranslate\"><span class=\"pre\">super()</span></code> with <code class=\"docutils literal notranslate\"><span class=\"pre\">kwargs</span></code> of <a class=\"reference external\" href=\"https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser\" title=\"(en Python versión 3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ArgumentParser</span></code></a> parameters.</p>\n</dd></dl>\n\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><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-exc docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a>, it’s intercepted and printed to <code class=\"docutils literal notranslate\"><span class=\"pre\">stderr</span></code>.</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=\"/es/5.2/ref/django-admin/#django.core.management.call_command\" title=\"django.core.management.call_command\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">call_command()</span></code></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 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>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">include_deployment_checks</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_level</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">checks.ERROR</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">databases</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</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><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-exc docutils literal notranslate\"><span class=\"pre\">CommandError</span></code></a>;\nwarnings are output to <code class=\"docutils literal notranslate\"><span class=\"pre\">stderr</span></code>; minor notifications are output to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code>.</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 except deployment and database related checks. <code class=\"docutils literal notranslate\"><span class=\"pre\">tags</span></code> can be a\nlist of check tags, like <code class=\"docutils literal notranslate\"><span class=\"pre\">compatibility</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">models</span></code>.</p>\n<p>You can pass <code class=\"docutils literal notranslate\"><span class=\"pre\">include_deployment_checks=True</span></code> to also perform deployment\nchecks, and list of database aliases in the <code class=\"docutils literal notranslate\"><span class=\"pre\">databases</span></code> to run database\nrelated checks against them.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.BaseCommand.get_check_kwargs\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_check_kwargs</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">options</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.management.BaseCommand.get_check_kwargs\"><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=\"5.2\">\n<p class=\"version-note-title\">New in Django 5.2</p></aside>\n<p>Supplies kwargs for the call to <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.check\" title=\"django.core.management.BaseCommand.check\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">check()</span></code></a>, including transforming the\nvalue of <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> to the <code class=\"docutils literal notranslate\"><span class=\"pre\">tag</span></code> kwarg.</p>\n<p>Override this method to change the values supplied to <a class=\"reference internal\" href=\"#django.core.management.BaseCommand.check\" title=\"django.core.management.BaseCommand.check\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">check()</span></code></a>. For\nexample, to opt into database related checks you can override\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_check_kwargs()</span></code> 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\">get_check_kwargs</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">options</span><span class=\"p\">):</span>\n    <span class=\"n\">kwargs</span> <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">get_check_kwargs</span><span class=\"p\">(</span><span class=\"n\">options</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"p\">{</span><span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">,</span> <span class=\"s2\">&quot;databases&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">options</span><span class=\"p\">[</span><span class=\"s2\">&quot;database&quot;</span><span class=\"p\">]]}</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"basecommand-subclasses\">\n<span id=\"ref-basecommand-subclasses\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">BaseCommand</span></code> 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=\"/es/5.2/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<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 attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.LabelCommand.label\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">LabelCommand.</span></span><span class=\"sig-name descname\"><span class=\"pre\">label</span></span><a class=\"heading-anchor\" href=\"#django.core.management.LabelCommand.label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string describing the arbitrary arguments passed to the command. The\nstring is used in the usage text and error messages of the command.\nDefaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">'label'</span></code>.</p>\n</dd></dl>\n\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</section>\n<section id=\"command-exceptions\">\n<h3>Excepciones de comando<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 exception\">\n<dt class=\"sig sig-object py\" id=\"django.core.management.CommandError\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">exception</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CommandError</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">returncode</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">1</span></span></em><span class=\"sig-paren\">)</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., <code class=\"docutils literal notranslate\"><span class=\"pre\">stderr</span></code>); as a result,\nraising this exception (with a sensible description of the error) is the\npreferred way to indicate that something has gone wrong in the execution of a\ncommand. It accepts the optional <code class=\"docutils literal notranslate\"><span class=\"pre\">returncode</span></code> argument to customize the exit\nstatus for the management command to exit with, using <a class=\"reference external\" href=\"https://docs.python.org/3/library/sys.html#sys.exit\" title=\"(en Python versión 3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">sys.exit()</span></code></a>.</p>\n<p>If a management command is called from code through\n<a class=\"reference internal\" href=\"/es/5.2/ref/django-admin/#django.core.management.call_command\" title=\"django.core.management.call_command\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">call_command()</span></code></a>, it’s up to you to catch the\nexception when needed.</p>\n</section>\n</section>","rootId":"module-django.core.management","toc":[{"title":"Aceptar los argumentos opcionales","anchor":"accepting-optional-arguments","children":[]},{"title":"Management commands and locales","anchor":"management-commands-and-locales","children":[]},{"title":"Testing","anchor":"testing","children":[]},{"title":"Sobreescribiendo comandos","anchor":"overriding-commands","children":[]},{"title":"Objetos de Comando","anchor":"command-objects","children":[{"title":"Atributos","anchor":"attributes","children":[]},{"title":"Métodos","anchor":"methods","children":[]},{"title":"BaseCommand subclasses","anchor":"basecommand-subclasses","children":[]},{"title":"Excepciones de comando","anchor":"command-exceptions","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"How-to guides","url":"/es/5.2/howto/"}],"prev":{"docname":"howto/custom-file-storage","title":"Cómo escribir una clase de almacenamiento personalizada","url":"/es/5.2/howto/custom-file-storage/"},"next":{"docname":"howto/custom-shell","title":"How to customize the shell command","url":"/es/5.2/howto/custom-shell/"},"formats":{"html":"/es/5.2/howto/custom-management-commands/","markdown":"/es/5.2/howto/custom-management-commands.md","json":"/es/5.2/howto/custom-management-commands.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/howto/custom-management-commands.txt","official":"https://docs.djangoproject.com/es/5.2/howto/custom-management-commands/","inVersions":["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"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}