GSOC 2023 Discussion: Auto-importing shell

Hello everyone. I’m Emeka, a third year student of Mathematics at the University of Lagos and I would love to contribute to this project by working on the new Auto-importing shell feature.

I’ve gone through the requirements for the feature as well as django_extensions implementation of this and I have a fairly good understanding of how it works. To corroborate this, I took my time to work on a skinned down version that just works. Here’s the PR. Please review and comment on it as well

My approach was to first get all the apps from the INSTALLED_APPS list, and then read the models.py file in each app. Yes, not all apps in there were created by the user, so I used a try-except to avoid that breaking anything. Then I read through each models.py file and sliced the string to get all the classes in each file. I then convert it to a class type and add it to the imported_objects dictionary. Which is then imported along with the exec command that brings up the python interpreter.

Then, I am not sure of “extra things to import”. I am assuming perhaps classes from the views or maybe functions. And I think they could follow the same pattern

Opting to get the model names from the admins.py file is an option I didn’t progress with because not every one uses the default admin page, and hence would not see a need to add it. Though we could decide to do that if reading each models.py file is not worth it.

I’m also welcome to better approaches to this problem from everyone!

Also looking forward to your comments on this @adamchainz

Hi @adamchainz. I would appreciate a review or comment as soon as you can.
Thanks