software - python django tutorial
What do I need to import to gain access to my models? (5)
Depending on your specific needs, django-command-extensions might save you a bit of time. To run any script as-is without messing around with environment variables just type:
./manage.py runscript path/to/my/script.py
django-command-extensions also has commands for automating scripts as cron jobs, which is something you mentioned that you'd like to do.
If you are a more nuts and bolts type of person, you might check out this very detailed post outlining how to make "standalone" django scripts to be run from cron jobs and whatnot.
I'd like to run a script to populate my database. I'd like to access it through the Django database API.
The only problem is that I don't know what I would need to import to gain access to this.
How can this be achieved?
If you use the
shell argument to the
manage.py script in your project directory, you don't have to import the settings manually:
$ cd mysite/ $ ./manage.py shell Python 2.5.2 (r252:60911, Jun 10 2008, 10:35:34) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from myapp.models import * >>>
For non-interactive use you could implement a custom command and run it with
The preferred way should be to add a custom command and then run it as any other
django-admin (not to be confused with
./manage.py mycustomcommand --customarg
Setting DJANGO_SETTINGS_MODULE should only be used when a custom command is not feasible.
This is what I have at the top of one my data loading scripts.
import string import sys try: import settings # Assumed to be in the same directory. #settings.DISABLE_TRANSACTION_MANAGEMENT = True except ImportError: sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) sys.exit(1) #Setup the django environment with the settings module. import django import django.core.management django.core.management.setup_environ(settings) from django.db import transaction
This should all execute before you do much else in your script.
Another method is to use fixtures and manage.py. Though if you are just trying to accomplish a bulk data load to initialize a database this should work fine.
Also depending on what you are doing you may or may not want to do it all in one transaction. Uncomment the transaction line above and structure your code similar to this.
transaction.enter_transaction_management() try: #Do some stuff transaction.commit() finally: transaction.rollback() pass transaction.leave_transaction_management()
Django: How can I use my model classes to interact with my database from outside Django?
You need to set up the Django environment variables. These tell Python where your project is, and what the name of the settings module is (the project name in the settings module is optional):
import os os.environ['PYTHONPATH'] = '/path/to/myproject' os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
Now you should be able to access the models:
from myproject.models import MyModel all_my_models = MyModel.objects.all()