Last active
May 10, 2019 00:54
-
-
Save mverteuil/98d2df647a9826ec6b54eaeaf7dd0e85 to your computer and use it in GitHub Desktop.
Enable the equivalent of ./manage.py shell_plus under PyCharm's Django Console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Confirmed working under: | |
# - Python3.7 / Django 2.2 / Django Extensions / IPython Available | |
# - Python3.7 / Django 2.2 / Django Extensions / IPython Unavailable | |
# | |
# Notices: | |
# - This might even work in PyCharm CE with the python console, but no guarantees there. | |
# - There are no language features required that shouldn't be available as far back as python2.6, maybe 2.5. | |
# - There are no framework features required that shouldn't be available as far back as Django 1.7 | |
# - Should still provide basic Django shell when Django Extensions is unavailable. | |
# - This is for all of my friends to enjoy! | |
# | |
# Installation: | |
# 1. PyCharm -> Preferences | |
# 2. Build, Execution, Deployment -> Console -> Django Console | |
# 3. Settings: | |
# - Project: Your Django Project | |
# - Python Interpreter: Your Django Project's VirtualEnv | |
# - Environment Variables, Interpreter Options, Working Directory: {NONE} | |
# - [x] Add content roots to PYTHONPATH, Add source roots to PYTHONPATH | |
# - Starting Script: {THE ENTIRE CONTENTS OF THIS FILE} | |
# 4. Tools -> Python Console | |
# 5. Enjoy! | |
# | |
# ------------------------------------------------------------------------------ | |
__author__ = "Matthew de Verteuil" | |
__copyright__ = "Copyright (C) 2019 Matthew de Verteuil" | |
__license__ = "GPL-3.0" | |
__version__ = "1.0" | |
import sys | |
import django | |
from django.core.management.color import color_style | |
# WORKING_DIR_AND_PYTHON_PATHS is injected by PyCharm | |
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS]) | |
# Change this value if you have any issues with prompt display | |
style = color_style(force_color=True) | |
try: | |
from django_extensions.management.shells import import_objects | |
imported_objects = import_objects({}, style) | |
except ImportError: | |
imported_objects = {} | |
try: | |
# Try activating rlcompleter, because it's handy. | |
import readline | |
except ImportError: | |
pass | |
else: | |
# We don't have to wrap the following import in a 'try', because | |
# we already know 'readline' was imported successfully. | |
import rlcompleter | |
readline.set_completer(rlcompleter.Completer(imported_objects).complete) | |
# Enable tab completion on systems using libedit (e.g. macOS). | |
# These lines are copied from Lib/site.py on Python 3.4. | |
readline_doc = getattr(readline, "__doc__", "") | |
if readline_doc is not None and "libedit" in readline_doc: | |
readline.parse_and_bind("bind ^I rl_complete") | |
else: | |
readline.parse_and_bind("tab:complete") | |
locals().update(imported_objects) | |
print(style.NOTICE("Python %s on %s" % (sys.version, sys.platform))) | |
print(style.NOTICE("Django %s" % django.get_version())) | |
print(style.SUCCESS("Shell Prepared. Enjoy!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment