summaryrefslogtreecommitdiff
path: root/docs
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
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')
-rw-r--r--docs/howto/custom-management-commands.txt2
-rw-r--r--docs/howto/custom-shell.txt57
-rw-r--r--docs/howto/index.txt3
-rw-r--r--docs/intro/tutorial02.txt6
-rw-r--r--docs/intro/tutorial05.txt1
-rw-r--r--docs/ref/django-admin.txt13
-rw-r--r--docs/releases/5.2.txt20
7 files changed, 96 insertions, 6 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::
diff --git a/docs/intro/tutorial02.txt b/docs/intro/tutorial02.txt
index d43c82c5d2..6a87d2d01c 100644
--- a/docs/intro/tutorial02.txt
+++ b/docs/intro/tutorial02.txt
@@ -347,13 +347,13 @@ API Django gives you. To invoke the Python shell, use this command:
We're using this instead of simply typing "python", because :file:`manage.py`
sets the :envvar:`DJANGO_SETTINGS_MODULE` environment variable, which gives
Django the Python import path to your :file:`mysite/settings.py` file.
+By default, the :djadmin:`shell` command automatically imports the models from
+your :setting:`INSTALLED_APPS`.
Once you're in the shell, explore the :doc:`database API </topics/db/queries>`:
.. code-block:: pycon
- >>> from polls.models import Choice, Question # Import the model classes we just wrote.
-
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
@@ -443,8 +443,6 @@ Save these changes and start a new Python interactive shell by running
.. code-block:: pycon
- >>> from polls.models import Choice, Question
-
# Make sure our __str__() addition worked.
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>
diff --git a/docs/intro/tutorial05.txt b/docs/intro/tutorial05.txt
index 921670aa5e..219b7b3130 100644
--- a/docs/intro/tutorial05.txt
+++ b/docs/intro/tutorial05.txt
@@ -150,7 +150,6 @@ whose date lies in the future:
>>> import datetime
>>> from django.utils import timezone
- >>> from polls.models import Question
>>> # create a Question instance with pub_date 30 days in the future
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> # was it published recently?
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 128abe5587..c393154d88 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -1065,6 +1065,19 @@ Mails the email addresses specified in :setting:`ADMINS` using
Starts the Python interactive interpreter.
+All models from installed apps are automatically imported into the shell
+environment. Models from apps listed earlier in :setting:`INSTALLED_APPS` take
+precedence. For a ``--verbosity`` of 2 or higher, the automatically imported
+objects will be listed. To disable automatic importing entirely, use the
+``--no-imports`` flag.
+
+See the guide on :ref:`customizing this behaviour
+<customizing-shell-auto-imports>` to add or remove automatic imports.
+
+.. versionchanged:: 5.2
+
+ Automatic models import was added.
+
.. django-admin-option:: --interface {ipython,bpython,python}, -i {ipython,bpython,python}
Specifies the shell to use. By default, Django will use IPython_ or bpython_ if
diff --git a/docs/releases/5.2.txt b/docs/releases/5.2.txt
index f1d68d234f..35ce053861 100644
--- a/docs/releases/5.2.txt
+++ b/docs/releases/5.2.txt
@@ -31,6 +31,26 @@ and only officially support the latest release of each series.
What's new in Django 5.2
========================
+Automatic models import in the ``shell``
+----------------------------------------
+
+The :djadmin:`shell` management command now automatically imports models from
+all installed apps. You can view further details of the imported objects by
+setting the ``--verbosity`` flag to 2 or more:
+
+.. code-block:: pycon
+
+ $ python -Wall manage.py shell --verbosity=2
+ 6 objects imported automatically, including:
+
+ from django.contrib.admin.models import LogEntry
+ from django.contrib.auth.models import Group, Permission, User
+ from django.contrib.contenttypes.models import ContentType
+ from django.contrib.sessions.models import Session
+
+This :ref:`behavior can be customized <customizing-shell-auto-imports>` to add
+or remove automatic imports.
+
Composite Primary Keys
----------------------