-
-
Save Quotation/909f87320b03b028d23c to your computer and use it in GitHub Desktop.
Use ptpython for Django shell
This file contains hidden or 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
| from django.core.management.commands.shell import Command as ShellCommand | |
| import os | |
| class Command(ShellCommand): | |
| shells = ShellCommand.shells.append('ptpython') | |
| def ptpython(self): | |
| try: | |
| # old ptpython | |
| from prompt_toolkit.contrib.repl import embed | |
| except ImportError: | |
| # new ptpython | |
| from ptpython.repl import embed | |
| history_filename = os.path.expanduser('~/.ptpython_history') | |
| embed(globals(), locals(), vi_mode=False, history_filename=history_filename) |
If you get:
TypeError: ptpython() takes 1 positional argument but 2 were given
try changing
def ptpython(self):
to
def ptpython(self, options):
Here's my full management/commands/shell.py file:
import os
from django.core.management.commands.shell import Command
from ptpython.repl import embed
def ptpython(self, options):
history_filename = os.path.expanduser("~/.ptpython_history")
embed(globals(), locals(), vi_mode=False, history_filename=history_filename)
Command.ptpython = ptpython
Command.shells.insert(0, "ptpython")
Append is incorrect should be ShellCommand.shells + ['ptpython']
Full version:
from django.core.management.commands.shell import Command as ShellCommand
import os
class Command(ShellCommand):
shells = ShellCommand.shells + ['ptpython']
def ptpython(self, options):
from ptpython.repl import embed
history_filename = os.path.expanduser('~/.ptpython_history')
embed(globals(), locals(), vi_mode=False, history_filename=history_filename)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First of all thanks for the code, I've been using this for a long period of time.
ptpythonprovides very convenient way of interactions with django apis. But unfortunately this does not work anymore, at least with Django version 2.2.6For me
./manage.py shell -i ptpythoncommand produces following traceback:So I searched for other solutions and found it on
ptpythonproject's github page: https://github.com/prompt-toolkit/ptpython#django-supportAll you need to do is install
django-extensionsalongside withptpython, and add it toINSTALLED_APPS. After that you can promptptpythonby next command:python manage.py shell_plus