Django Django from the command line.


Supposing you have setup a django project, and the settings file is in an app named main, this is how you initialize your code

import os, sys

# Setup environ
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")

# Setup django
import django

# rest of your imports go here

from main.models import MyModel

# normal python code that makes use of Django models go here

for obj in MyModel.objects.all():
    print obj

The above can be executed as

python main/