Django Shell Plus - Monadical-SAS/oddslingers.poker GitHub Wiki

Add this to your ~/.pythonrc to use bpython or ipython for an easy admin shell with autocompletion.

# Load django environment and all models
import django
oldpath = os.getcwd()
try:
    while "manage.py" not in os.listdir(os.getcwd()) and os.getcwd() != "/":
        os.chdir("..")

    if not "manage.py" in os.listdir(os.getcwd()):
        raise Exception("Not in a django dir.")

    # set up django settings
    sys.path.append(os.getcwd())
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "grater.settings")
    os.environ.setdefault("django_debug_helpers", "1")  # enables functions wrapped by @debug_only
    from grater import settings
    django.setup()

    try:
        # import all models in INSTALLED_APPS
        exec_strs = ["import %s" % app for app in settings.INSTALLED_APPS ] + ["from %s.models import *" % app for app in settings.INSTALLED_APPS ]
        for x in exec_strs:
            try:
                exec(x)
            except Exception:
                pass
    except Exception:
        pass

except Exception as e:
    # dont mess with path if not in a django dir
    os.chdir(oldpath)

Example usage in iPython/bpython:

cd ~/projects/oddslingers.poker/core/ui
pip install --upgrade bpython ipython
bpython
>>>User.objects.all()
<QuerySet []>
>>>t = PokerTable.objects.get(name='Homepage Table')
>>>t.pid
'55094'
>>>t.controller.step()
>>>t.players_set