From a5cd84ad2002f9a43363ab9fd9d9f6e9dfa48c60 Mon Sep 17 00:00:00 2001 From: Salvo Polizzi Date: Fri, 14 Feb 2025 08:17:25 +0100 Subject: Fixed #35680 -- Added automatic imports of common utilies to shell management command. --- django/core/management/commands/shell.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'django') diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py index 132e7f89d2..25edc80f7c 100644 --- a/django/core/management/commands/shell.py +++ b/django/core/management/commands/shell.py @@ -124,13 +124,19 @@ class Command(BaseCommand): def get_auto_imports(self): """Return a sequence of import paths for objects to be auto-imported. - By default, import paths for models in INSTALLED_APPS are included, - with models from earlier apps taking precedence in case of a name - collision. + By default, import paths for models in INSTALLED_APPS and some common + utilities are included, with models from earlier apps taking precedence + in case of a name collision. For example, for an unchanged INSTALLED_APPS, this method returns: [ + "django.conf.settings", + "django.db.connection", + "django.db.reset_queries", + "django.db.models", + "django.db.models.functions", + "django.utils.timezone", "django.contrib.sessions.models.Session", "django.contrib.contenttypes.models.ContentType", "django.contrib.auth.models.User", @@ -140,7 +146,15 @@ class Command(BaseCommand): ] """ - app_models_imports = [ + default_imports = [ + "django.conf.settings", + "django.db.connection", + "django.db.reset_queries", + "django.db.models", + "django.db.models.functions", + "django.utils.timezone", + ] + app_models_imports = default_imports + [ f"{model.__module__}.{model.__name__}" for model in reversed(apps.get_models()) if model.__module__ -- cgit v1.3