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!
You can install or upgrade via pip
pip install -U django-orm-magic
or directly from the repository using %install_ext
magic command.
# 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
Then you are ready to load the magic
%load_ext django_orm_magic
To load it each time IPython starts, list it in your configuration file:
c.InteractiveShellApp.extensions = [
'django_orm_magic'
]
You are ready to define your models starting with the cell magic %%django_orm
%%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
Poll.objects.all()
[]
from django.utils import timezone
p = Poll(question="What's new?", pub_date=timezone.now())
p.save()
Poll.objects.all()
[<Poll: Poll object>]
_.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.%django_settings
%%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.
orm_magic
from INSTALLED_APPS
. This is the app that will contains your own models.
%%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
%%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)
from django.contrib.auth.models import User # TO DO: load any third party model used automatically
User.objects.create(username='mgaitan')
<User: mgaitan>
To restore the default settings
%django_settings --default
Deleted custom settings. Back to default for %%django_orm
%django_settings
%%django_settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite' #':memory:'
}
}
INSTALLED_APPS = ("orm_magic",)