I have a fairly straight forward example of how to setup the Django environment and allow the rest of your code to access the Django framework for your web application. Its remarkably simple and straight forward.
Suppose that I've created a Django project in my tmp directory called demo_scripts and within that project, I create an app called someapp.
peter@fog:~/tmp> django-admin-2.5.py startproject demo_scripts
peter@fog:~/tmp> cd demo_scripts/
peter@fog:~/tmp/demo_scripts> django-admin-2.5.py startapp someapp
I create a model in someapp that looks like:
from django.db import models
name = models.CharField(max_length=21,
help_text="Name of the foo.")
ordering = ('name',)
Next step is to sync the database:
peter@fog:~/tmp/demo_scripts> ./manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table someapp_foo
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'peter'):
E-mail address: firstname.lastname@example.org
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
And add some initial data to the database:
peter@fog:~/tmp/demo_scripts> ./manage.py shell
Python 2.5.4 (r254:67916, May 1 2009, 17:14:50)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from someapp.models import Foo
>>> Foo(name='A Foo').save()
>>> Foo(name='Another Foo').save()
Now we can write a standalone script to do something with the data model. For simplicity's sake, I'll just print out all the Foo objects. The script is going to live in a new directory called scripts. Here's the source:
#! /usr/bin/env python
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from someapp.models import *
When I run the script, it prints the array of the two Foo objects that I previously created:
[<Foo: A Foo>, <Foo: Another Foo>]
Lines 8 and 9 are the critical lines in the script code. The first adds the project directory to the Python system path so that the settings module can be found. The second tells the Django code which module to import to determine the project settings.