[Django]-Automatically import models on Django shell launch

67👍

install django-extensions, one of the commands it features (shell_plus) is providing the context for your models.
https://github.com/django-extensions/django-extensions

So, instead of ./manage.py shell you can use ./manage.py shell_plus so that everything is imported.

8👍

http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP

If you set the environment variable PYTHONSTARTUP to a file, this will be run first whenever you start a python shell.

0👍

Create a file.py file in the same directory as manage.py file with all imports that you need.
For example:

from django.core.cache import cache; from django.db.models import Prefetch, Count, Q; from django.db import connection, reset_queries; from datetime import timedelta; from django.utils import timezone;

Then, every time after you launch the shell, simply import that file: from file import *

Leave a comment