[GSOC 2024] Auto-importing in the shell

Hello everyone!

Thank you @salvo-polizzi for working on this. I’m in the process of reviewing the PR and it looks great. Thanks also to @adamchainz and @DevilsAutumn for mentoring this feature!

I know I may be a bit late to the discussion (I couldn’t engage earlier – sorry about that), but I wanted to share some thoughts about the items that are automatically imported. This feedback comes after using and reviewing this work. Above all, I want to respect the previous consensus, but I also want to bring the following to the table for further consideration:

I would prefer not to have the automatic import of the models module for each INSTALLED_APPS aliased to appname_models.

My understanding is that this was added to help with cases where colliding model names could potentially cause some models to be unavailable in the shell. However, I think that in everyday Django work, having:

  • colliding model names
  • that are needed in the console
  • during (likely) debugging

feels like a minority of cases. A Django user can always manually import it with something like from myapp.models import CollidingName as CollidingNameNewer.

The downside of the current approach, where these models are always imported with an alias, is that it clutters the namespace and disrupts the clean and beatiful printing of the imported modules in the shell’s “new preamble”.

To exemplify my point, this is the current proposal (run in a very small pet project of mine, with isort installed):

But I rather something more in line with this:

As a reference, shell_plus (by default) does the following:

(Name collisions in shell_plus are handled via collision resolvers, I don’t think we need such a complex mechanism either.)

Lastly, to further support my proposal, I’ll quote @carltongibson from this post:

Simple, to the point, covers the 80% case is probably enough for an addition to Django itself here, leaving space in the ecosystem for folks to explore more advanced options.

Thanks everyone!