Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preload imports into Django's manage.py shell command?

When I run manage.py shell on my Django project to take a peek at something there are common imports that I always want to run at the start of the shell (e.g. I always want to import my model files.) How can I have these run automatically everytime I run the shell command?

2nd related question, when I hit the up arrow I get the "^A" character instead of the previously run command in the manage.py shell (and in my regular python shell), how can I fix this so it loads the previous command like on the Linux/Unix command line?

like image 509
MikeN Avatar asked Dec 02 '09 22:12

MikeN


People also ask

What does the Django command manage py shell do?

In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.

How do I run manage py?

To run a task of the manage.py utilityOn the main menu, choose Tools | Run manage.py task, or press Ctrl+Alt+R . The manage.py utility starts in its own console. Type the name of the desired task.

What is manage py file in Django?

Manage.py in Django is a command-line utility that works similar to the django-admin command. The difference is that it points towards the project's settings.py file. This manage.py utility provides various commands that you must have while working with Django.

Where is Django-admin py located?

The django-admin.py script should be on your system path if you installed Django via its setup.py utility. If it's not on your path, you can find it in site-packages/django/bin within your Python installation.


1 Answers

Auto importing frequently used packages in python manage.py shell_plus

Example:

#  local_settings
SHELL_PLUS_PRE_IMPORTS = (
    ('<app_name>.models', '*'),
    ('<app_name>.forms', '*'),
    ('<app_name>.views', '*'),
    ('django.core.urlresolvers', '*'),
    ('django.db', 'connection'),
    ('django.db', 'reset_queries'),
)

SHELL_PLUS_DONT_LOAD = ['<app_name>', '<app_name>']

Reference:
https://github.com/django-extensions/django-extensions/blob/master/docs/shell_plus.rst

like image 56
Parag Tyagi Avatar answered Sep 22 '22 07:09

Parag Tyagi