Automatically import all Django models in the Django Console in PyCharm
I frequently use the built-in Django Console in PyCharm to test Django ORM queries and other bits of code. It is much nicer than running the Django shell from the terminal, as you get syntax highlighting, code completion and more.
You can customize this console to automatically import all Django models to save you some repetitive typing, similar to the shell_plus management command from the django-extensions package.
In PyCharm, open the Settings/Preferences dialog and select Build, Execution, Deployment | Console | Django Console. By default, the Starting script already contains a few lines of Python code to setup the Django shell:
To simply load all models from every app in INSTALLED_APPS
, including models from Django and third-party packages, add the following lines to the Starting script:
If you only want to load models from your own apps, you can add something like this:
You can also import other objects that you regularly use. For example, I have added from datetime import date, datetime, timedelta
to the Starting script as well.