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.
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.
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!
In [1]:
# https://raw.github.com/mgaitan/django-orm-magic/master/django_orm_magic.py
%install_ext django_orm_magic.py
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'
]
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]:
In [7]:
_.delete()
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.
In [4]:
%%django_settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
}
INSTALLED_APPS = ("django.contrib.auth", "django.contrib.contenttypes", "orm_magic",)
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]:
To restore the default settings
In [8]:
%django_settings --default
In [9]:
%django_settings
In [ ]:
%%django_settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite' #':memory:'
}
}
INSTALLED_APPS = ("orm_magic",)
In [ ]: