summaryrefslogtreecommitdiff
path: root/docs/howto
diff options
context:
space:
mode:
authorSalvo Polizzi <101474753+salvo-polizzi@users.noreply.github.com>2025-01-09 17:00:29 +0100
committerGitHub <noreply@github.com>2025-01-09 13:00:29 -0300
commitfc28550fe4e0582952993976edc62971bd5345a8 (patch)
treecaf9428df753d1266dec97cffb3d4571cceacab7 /docs/howto
parent8c118c0e00846091c261b97dbed9a5b89ceb79bf (diff)
Fixed #35515 -- Added automatic model imports to shell management command.
Thanks to Bhuvnesh Sharma and Adam Johnson for mentoring this Google Summer of Code 2024 project. Thanks to Sarah Boyce, David Smith, Jacob Walls and Natalia Bidart for reviews.
Diffstat (limited to 'docs/howto')
-rw-r--r--docs/howto/custom-management-commands.txt2
-rw-r--r--docs/howto/custom-shell.txt57
-rw-r--r--docs/howto/index.txt3
3 files changed, 61 insertions, 1 deletions
diff --git a/docs/howto/custom-management-commands.txt b/docs/howto/custom-management-commands.txt
index de6c38c1e0..ce8c85276c 100644
--- a/docs/howto/custom-management-commands.txt
+++ b/docs/howto/custom-management-commands.txt
@@ -157,6 +157,8 @@ Testing
Information on how to test custom management commands can be found in the
:ref:`testing docs <topics-testing-management-commands>`.
+.. _overriding-commands:
+
Overriding commands
===================
diff --git a/docs/howto/custom-shell.txt b/docs/howto/custom-shell.txt
new file mode 100644
index 0000000000..07034dd197
--- /dev/null
+++ b/docs/howto/custom-shell.txt
@@ -0,0 +1,57 @@
+======================================
+How to customize the ``shell`` command
+======================================
+
+The Django :djadmin:`shell` is an interactive Python environment that provides
+access to models and settings, making it useful for testing code, experimenting
+with queries, and interacting with application data.
+
+Customizing the :djadmin:`shell` command allows adding extra functionality or
+pre-loading specific modules. To do this, create a new management command that subclasses
+``django.core.management.commands.shell.Command`` and overrides the existing
+``shell`` management command. For more details, refer to the guide on
+:ref:`overriding commands <overriding-commands>`.
+
+.. _customizing-shell-auto-imports:
+
+Customize automatic imports
+===========================
+
+.. versionadded:: 5.2
+
+To customize the automatic import behavior of the :djadmin:`shell` management
+command, override the ``get_namespace()`` method. For example:
+
+.. code-block:: python
+ :caption: ``polls/management/commands/shell.py``
+
+ from django.core.management.commands import shell
+
+
+ class Command(shell.Command):
+ def get_namespace(self):
+ from django.urls.base import resolve, reverse
+
+ return {
+ **super().get_namespace(),
+ "resolve": resolve,
+ "reverse": reverse,
+ }
+
+The above customization adds :func:`~django.urls.resolve` and
+:func:`~django.urls.reverse` to the default namespace, which includes all
+models from all apps. These two functions will then be available when the
+shell opens, without a manual import statement.
+
+If you prefer to not have models automatically imported, create a custom
+``get_namespace()`` that excludes the ``super().get_namespace()`` call:
+
+.. code-block:: python
+ :caption: ``polls/management/commands/shell.py``
+
+ from django.core.management.commands import shell
+
+
+ class Command(shell.Command):
+ def get_namespace(self):
+ return {}
diff --git a/docs/howto/index.txt b/docs/howto/index.txt
index d799ca7906..d49a9b1206 100644
--- a/docs/howto/index.txt
+++ b/docs/howto/index.txt
@@ -58,8 +58,9 @@ Other guides
auth-remote-user
csrf
- custom-management-commands
custom-file-storage
+ custom-management-commands
+ custom-shell
.. seealso::