File: custom-shell.txt

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (67 lines) | stat: -rw-r--r-- 2,638 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
======================================
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_auto_imports()`` method. This method should return
a sequence of import paths for objects or modules available in the application.
For example:

.. code-block:: python
    :caption: ``polls/management/commands/shell.py``

    from django.core.management.commands import shell


    class Command(shell.Command):
        def get_auto_imports(self):
            return super().get_auto_imports() + [
                "django.urls.reverse",
                "django.urls.resolve",
            ]

The customization above adds :func:`~django.urls.resolve` and
:func:`~django.urls.reverse` to the default namespace, which already includes
all models from the apps listed in :setting:`INSTALLED_APPS`. These objects
will be available in the ``shell`` without requiring a manual import.

Running this customized ``shell`` command with ``verbosity=2`` would show:

.. console::

    8 objects imported automatically:

      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
      from django.urls import resolve, reverse

If an overridden ``shell`` command includes paths that cannot be imported,
these errors are shown when ``verbosity`` is set to ``1`` or higher. Duplicate
imports are automatically handled.

Note that automatic imports can be disabled for a specific ``shell`` session
using the :option:`--no-imports <shell --no-imports>` flag. To permanently
disable automatic imports, override ``get_auto_imports()`` to return ``None``::

    class Command(shell.Command):
        def get_auto_imports(self):
            return None