{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Django ORM Magic\n", "\n", "django-orm-magic is an [IPython](http://ipython.org) extension that help to use the django ORM in your interactive session. \n", "\n", "Define your django models in an IPython cell and use them on the fly. Let the magic do the boring part.\n", "\n", "##Introduction\n", "\n", "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.\n", "\n", "\n", "## How it works\n", "\n", "It adds a `%%django_orm` cell magic that handle all the setup needed to use django models on the fly\n", "\n", "\n", "* Author: Mart\u00edn Gait\u00e1n ( gaitan AT gmail.com ) \n", "* Homepage: https://github.com/mgaitan/django-orm-magic\n", "* License: BSD\n", "\n", "Feedback, report of issues and pull requests are welcome!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install or upgrade\n", "\n", "You can install or upgrade via `pip`\n", "\n", " pip install -U django-orm-magic\n", "\n", "or directly from the repository using `%install_ext` magic command." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# https://raw.github.com/mgaitan/django-orm-magic/master/django_orm_magic.py\n", "%install_ext django_orm_magic.py" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Installed django_orm_magic.py. To use it, type:\n", " %load_ext django_orm_magic\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load \n", "\n", "Then you are ready to load the magic " ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext django_orm_magic" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To load it each time IPython starts, list it in your configuration file:\n", "\n", " c.InteractiveShellApp.extensions = [\n", " 'django_orm_magic'\n", " ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic usage\n", "\n", "You are ready to define your models starting with the cell magic ``%%django_orm``\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%django_orm\n", "\n", "from django.db import models\n", "\n", "class Poll(models.Model):\n", " question = models.CharField(max_length=200)\n", " pub_date = models.DateTimeField('date published')\n", "\n", "class Choice(models.Model):\n", " poll = models.ForeignKey(Poll)\n", " choice_text = models.CharField(max_length=200)\n", " votes = models.IntegerField(default=0)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And it's done. By default, every model is synced in a sqlite database named ``db.sqlite`` in your current path and imported automatically" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Poll.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "[]" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "from django.utils import timezone\n", "p = Poll(question=\"What's new?\", pub_date=timezone.now())\n", "p.save()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "Poll.objects.all()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "[]" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "_.delete()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom settings\n", "\n", "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``. \n", "\n", "``%django_settings`` could be used in three ways: \n", "\n", "* ``%django_settings`` alone, load the current settings as a template in a new cell\n", "* ``%django_settings --default`` delete any customization and return to the default settings\n", "* ``%%django_settings`` take the content of the cell as the new settings. \n", "\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%django_settings" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "%%django_settings\n", "\n", "DATABASES = {\n", " 'default': {\n", " 'ENGINE': 'django.db.backends.sqlite3',\n", " 'NAME': 'db.sqlite' #':memory:'\n", " }\n", " }\n", "INSTALLED_APPS = (\"orm_magic\",)\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then you can edit the cell above with your own settings. \n", "\n", "\n", "
\n", " Attention! Don't remove `orm_magic` from `INSTALLED_APPS`. This is the app that will contains your own models.\n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%django_settings\n", "\n", "DATABASES = {\n", " 'default': {\n", " 'ENGINE': 'django.db.backends.sqlite3',\n", " 'NAME': ':memory:'\n", " }\n", " }\n", "INSTALLED_APPS = (\"django.contrib.auth\", \"django.contrib.contenttypes\", \"orm_magic\",)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Settings for %%django_orm configured succesfully\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "%%django_orm\n", "\n", "from django.db import models\n", "from django.contrib.auth.models import User\n", "\n", "class Poll2(models.Model):\n", " user = models.ForeignKey(User)\n", " question = models.CharField(max_length=200)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "from django.contrib.auth.models import User # TO DO: load any third party model used automatically" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "User.objects.create(username='mgaitan')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To restore the default settings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%django_settings --default" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Deleted custom settings. Back to default for %%django_orm\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "%django_settings" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "%%django_settings\n", "\n", "DATABASES = {\n", " 'default': {\n", " 'ENGINE': 'django.db.backends.sqlite3',\n", " 'NAME': 'db.sqlite' #':memory:'\n", " }\n", " }\n", "INSTALLED_APPS = (\"orm_magic\",)\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }