# \[GSOC 2024\] Auto-importing in the shell

**URL:** <https://forum.djangoproject.com/t/gsoc-2024-auto-importing-in-the-shell/30749>\
**Category:** Google Season of Code\
**Created:** [May 2, 2024, 7:22am UTC](https://forum.djangoproject.com/t/gsoc-2024-auto-importing-in-the-shell/30749 "2024-05-02T07:22:42Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 14, 2024, 8:56pm UTC](https://forum.djangoproject.com/t/gsoc-2024-auto-importing-in-the-shell/30749/3 "2024-05-14T20:56:46Z")

</div>

Hi @salvo-polizzi , congratulations on being accepted. I proposed this project last year and I’m glad to see it is being picked up. I am not your mentor because I failed to log onto the GSoC panel, but I’m on hand to review your PRs.

Please review the past discussion with another potential candidate: [Have Solved 80% of auto\_importing Shell feature](https://forum.djangoproject.com/t/have-solved-80-of-auto-importing-shell-feature/27782) , and their PR: [[gsoc] django\_auto\_import shell feature by Ammar-Munirr · Pull Request #17826 · django/django · GitHub](https://github.com/django/django/pull/17826) .

In terms of features, I propose we don’t directly copy `shell_plus`. Instead, we should try to build a minimal, extensible design.

django-extensions’ design has “grown by committee” (or anarchy). Pretty much all PRs were merged, meaning its features are pretty haphazard. Its code shows that internally, with untested blocks, “secret” undocumented features, and general messiness. We’d rather make fewer solid features in Django, so we can maintain them for a long time.

Here’s how I’d refine Bhuvnesh’s suggestions:

1. The `shell` command gains a new method, called, say, `get_namespace()`. This method returns a dictionary of names to objects, which are all added into the namespace.

2. The default implementation of `get_namespace()` adds all models from [`apps.get_models()`](https://github.com/django/django/blob/f030236a86a64a4befd3cc8093e2bbeceef52a31/django/apps/registry.py#L169). We can have some debate about what to do with name collisions. But whatever we do, models in earlier apps need to take precedence, as that’s the precedence order used elsewhere, such as for management command lookup.

3. The `SHELL_PLUS_IMPORTS` is not needed. Instead, we document how to extend `get_namespace()` to add other imports, something like:

```python
# myapp/management/commands/shell.py
from django.core.management.commands import shell

class Command(shell.Command):
    def get_namespace(self):
        from django.core.urls import resolve, reverse
        
        return {
            **super().get_namespace(),
            "resolve": resolve,
            "reverse": reverse,
        }

```

1. No feature to “display all imported objects”, nor an option to turn it on. I think we should stick with a simple message like “X objects automatically imported.”. Users can use `globals()` or shell-specific features to see all imported objects, if and when they care.

2. Copying the functionality from `SHELL_PLUS_PRINT_SQL` is out of scope.

3. This functionality should work in the supported shells as much as possible.

4. There’s no need to add support for extending with other shells, as that already exists. One can override the command with a subclass and add an extra method with its name in [the `shells` attribute](https://github.com/django/django/blob/f030236a86a64a4befd3cc8093e2bbeceef52a31/django/core/management/commands/shell.py#L18). But this is undocumented, so it would be a nice bonus to make a side PR documenting how to use this feature.

Thanks again for working on this project, I’m excited to see how it goes!

---

_[View the full topic](https://forum.djangoproject.com/t/gsoc-2024-auto-importing-in-the-shell/30749)._
