Django ORM Magic

django-orm-magic is an IPython extension that help to use the django ORM in your interactive session.

Define your django models in an IPython cell and use them on the fly. Let the magic do the boring part.

Introduction

Django ORM isn't not conceived to be used standalone. Even for a trivial use case, you need to configure a database, create an app, setup an eviroment variable, etc. This magic handle that steps automatically and then import every model ready to use in your interactive session.

How it works

It adds a %%django_orm cell magic that handle all the setup needed to use django models on the fly

Feedback, report of issues and pull requests are welcome!

Install or upgrade

You can install or upgrade via pip

    pip install -U django-orm-magic

or directly from the repository using %install_ext magic command.


In [1]:
# https://raw.github.com/mgaitan/django-orm-magic/master/django_orm_magic.py
%install_ext django_orm_magic.py


Installed django_orm_magic.py. To use it, type:
  %load_ext django_orm_magic

Load

Then you are ready to load the magic


In [2]:
%load_ext django_orm_magic

To load it each time IPython starts, list it in your configuration file:

c.InteractiveShellApp.extensions = [
    'django_orm_magic'
]

Basic usage

You are ready to define your models starting with the cell magic %%django_orm


In [3]:
%%django_orm

from django.db import models

class Poll(models.Model):
   question = models.CharField(max_length=200)
   pub_date = models.DateTimeField('date published')

class Choice(models.Model):
   poll = models.ForeignKey(Poll)
   choice_text = models.CharField(max_length=200)
   votes = models.IntegerField(default=0)

And it's done. By default, every model is synced in a sqlite database named db.sqlite in your current path and imported automatically


In [4]:
Poll.objects.all()


Out[4]:
[]

In [5]:
from django.utils import timezone
p = Poll(question="What's new?", pub_date=timezone.now())
p.save()

In [6]:
Poll.objects.all()


Out[6]:
[<Poll: Poll object>]

In [7]:
_.delete()

Custom settings

If you want to connect to another database, add third django apps or so, you can customize the settings.py file using the magic %django_settings.

%django_settings could be used in three ways:

  • %django_settings alone, load the current settings as a template in a new cell
  • %django_settings --default delete any customization and return to the default settings
  • %%django_settings take the content of the cell as the new settings.

In [3]:
%django_settings

In [ ]:
%%django_settings

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': 'db.sqlite' #':memory:'
    }
  }
INSTALLED_APPS = ("orm_magic",)

Then you can edit the cell above with your own settings.


Attention! Don't remove `orm_magic` from `INSTALLED_APPS`. This is the app that will contains your own models.


In [4]:
%%django_settings

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': ':memory:'
    }
  }
INSTALLED_APPS = ("django.contrib.auth", "django.contrib.contenttypes", "orm_magic",)


Settings for %%django_orm configured succesfully

In [5]:
%%django_orm

from django.db import models
from django.contrib.auth.models import User

class Poll2(models.Model):
   user = models.ForeignKey(User)
   question = models.CharField(max_length=200)

In [6]:
from django.contrib.auth.models import User     # TO DO: load any third party model used automatically

In [7]:
User.objects.create(username='mgaitan')


Out[7]:
<User: mgaitan>

To restore the default settings


In [8]:
%django_settings --default


Deleted custom settings. Back to default for %%django_orm

In [9]:
%django_settings

In [ ]:
%%django_settings

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': 'db.sqlite' #':memory:'
    }
  }
INSTALLED_APPS = ("orm_magic",)

In [ ]: