{"title":"Comment personnaliser la commande shell","version":"6.1","locale":"fr","docname":"howto/custom-shell","url":"/fr/6.1/howto/custom-shell/","canonical":"https://djangodocs.dev/fr/6.1/howto/custom-shell/","summary":"Le shell Django est un environnement Python interactif donnant accès aux modèles et aux réglages, ce qui en fait un outil important pour tester du code…","html":"<h1>Comment personnaliser la commande <code class=\"docutils literal notranslate\">shell</code><a class=\"heading-anchor\" href=\"#how-to-customize-the-shell-command\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Le <a class=\"reference internal\" href=\"/fr/6.1/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\">shell</code></a> Django est un environnement Python interactif donnant accès aux modèles et aux réglages, ce qui en fait un outil important pour tester du code, expérimenter avec des requêtes ou pour interagir avec les données d’une application.</p>\n<p>La personnalisation de la commande <a class=\"reference internal\" href=\"/fr/6.1/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\">shell</code></a> permet d’ajouter des fonctionnalités supplémentaires ou de précharger des modules spécifiques. Pour cela, créez une nouvelle commande d’administration héritant de <code class=\"docutils literal notranslate\">django.core.management.commands.shell.Command</code> et surchargez la commande <code class=\"docutils literal notranslate\">shell</code> existante. Pour plus de détails, référez-vous au guide sur la <a class=\"reference internal\" href=\"/fr/6.1/howto/custom-management-commands/#overriding-commands\"><span class=\"std std-ref\">surcharge des commandes</span></a>.</p>\n<section id=\"customize-automatic-imports\">\n<span id=\"customizing-shell-auto-imports\"></span><h2>Personnalisation des importations automatiques<a class=\"heading-anchor\" href=\"#customize-automatic-imports\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Pour personnaliser le comportement d’importation automatique de la commande d’administration <a class=\"reference internal\" href=\"/fr/6.1/ref/django-admin/#django-admin-shell\"><code class=\"xref std std-djadmin docutils literal notranslate\">shell</code></a>, surchargez la méthode <code class=\"docutils literal notranslate\">get_auto_imports()</code>. Cette méthode doit renvoyer une séquence de chemins d’importation pour des objets et des modules disponibles dans l’application. Par exemple :</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">polls/management/commands/shell.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.management.commands</span> <span class=\"kn\">import</span> shell\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Command</span><span class=\"p\">(</span>shell<span class=\"o\">.</span>Command<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">get_auto_imports</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>get_auto_imports<span class=\"p\">()</span> <span class=\"o\">+</span> <span class=\"p\">[</span>\n            <span class=\"s2\">&quot;django.urls.reverse&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;django.urls.resolve&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">]</span>\n</code></pre></figure>\n<p>La personnalisation ci-dessus ajoute <a class=\"reference internal\" href=\"/fr/6.1/ref/urlresolvers/#django.urls.resolve\" title=\"django.urls.resolve\"><code class=\"xref py py-func docutils literal notranslate\">resolve()</code></a> and <a class=\"reference internal\" href=\"/fr/6.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\">reverse()</code></a> à l’espace de noms par défaut, qui contient déjà tous les modèles des applications présentes dans <a class=\"reference internal\" href=\"/fr/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>, en plus de ce qui est importé par défaut. Ces objets seront disponibles dans le <code class=\"docutils literal notranslate\">shell</code> sans devoir les importer manuellement.</p>\n<p>Le lancement de cette commande <code class=\"docutils literal notranslate\">shell</code> personnalisée avec <code class=\"docutils literal notranslate\">verbosity=2</code> va afficher :</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"go\">13 objects imported automatically:</span>\n\n<span class=\"go\">  from django.db import connection, reset_queries, models</span>\n<span class=\"go\">  from django.conf import settings</span>\n<span class=\"go\">  from django.contrib.admin.models import LogEntry</span>\n<span class=\"go\">  from django.contrib.auth.models import Group, Permission, User</span>\n<span class=\"go\">  from django.contrib.contenttypes.models import ContentType</span>\n<span class=\"go\">  from django.contrib.sessions.models import Session</span>\n<span class=\"go\">  from django.urls import resolve, reverse</span>\n<span class=\"go\">  from django.utils import timezone</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>Les importations automatiques d’utilitaires communs, tels que <code class=\"docutils literal notranslate\">django.conf.settings</code>, ont été ajoutées.</p>\n</aside>\n<p>Si une commande <code class=\"docutils literal notranslate\">shell</code> personnalisée inclut des chemins qui ne peuvent pas être importés, ces erreurs sont affichées si <code class=\"docutils literal notranslate\">verbosity</code> est à <code class=\"docutils literal notranslate\">1</code> ou plus. Les importations à double sont gérées automatiquement.</p>\n<p>Notez que les importations automatiques peuvent être désactivées pour une session <code class=\"docutils literal notranslate\">shell</code> spécifique en utilisant l’option <a class=\"reference internal\" href=\"/fr/6.1/ref/django-admin/#cmdoption-shell-no-imports\"><code class=\"xref std std-option docutils literal notranslate\">--no-imports</code></a>. Pour désactiver les importations automatiques de manière permanente, surchargez <code class=\"docutils literal notranslate\">get_auto_imports()</code> pour qu’elle renvoie <code class=\"docutils literal notranslate\">None</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=\"nc\">Command</span><span class=\"p\">(</span>shell<span class=\"o\">.</span>Command<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">get_auto_imports</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"kc\">None</span>\n</code></pre></div>\n</section>","rootId":"how-to-customize-the-shell-command","toc":[{"title":"Personnalisation des importations automatiques","anchor":"customize-automatic-imports","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Guides pratiques","url":"/fr/6.1/howto/"}],"prev":{"docname":"howto/custom-management-commands","title":"Écriture de commandes django-admin personnalisées","url":"/fr/6.1/howto/custom-management-commands/"},"next":{"docname":"howto/mailers-migration","title":"Migration des réglages de messagerie vers le système des envois","url":"/fr/6.1/howto/mailers-migration/"},"formats":{"html":"/fr/6.1/howto/custom-shell/","markdown":"/fr/6.1/howto/custom-shell.md","json":"/fr/6.1/howto/custom-shell.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/howto/custom-shell.txt","official":"https://docs.djangoproject.com/fr/6.1/howto/custom-shell/","inVersions":["6.1","6.0","5.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}