Using Django Models without Using Django
by Overmortal on 2008-11-03 11:08:03tags: django, orm, python, web site development
Not all of the web sites we do are full-board software applications. Some of these web sites are simple brochure-ware for local area businesses. Although we could overcharge for a full-suite content management system and hand out chainsaws to cut butter (like some of our competitors), that's just not our style. We try to fulfill our clients' actual technology needs, and sometimes, that need is just a plain old web site with a handful of pages.
Some brochure-ware sites do require a bit of dynamic coding though - such as a web site that requires newsletter signup functionality. This functionality might be enough to require a scripting language, but it might not be enough to warrant the overhead of a full-blown framework.
As a business serving your customers' needs, you often have to find a balance between necessity and convenience. If I simply told a client "you need to use this framework," chances are they would listen to me. They would listen to me because they trust me and the work of my team. That's a lot of power, and it's important not to abuse that power - hence the "balance" that must be found between the needs of the client and the convenience of the technology teams' workflow.
When I do small to medium-sized web sites that need some simple dynamic programming, I like to use Python. It's fast and reliable - so even if I don't use the Python Django framework to build the web site, I still end up with Python code in the project.
Django, however, does a much better job at handling database functionality with its ORM models. Wouldn't it be nice to be able to use Django models without having the overhead of the framework? You can.
Let's say you do need that newsletter functionality mentioned earlier, and you have a newsletter.py file to run the code, but you want to uses Django's models to save the emails into the database. You'll need a vanilla Django settings file, a blank init file and a models.py file that describes your database. I stuff my settings file and all Django related files into their own subdirectory, so it looks like this:
[subdir]
|--- __init__.py (blank init file) |--- settings.py |--- models.py
In your newsletter.py, file you need to first import Django's setup_environ() method and your settings file:
from django.core.management import setup_environ import subdir.settings
You'll then need to pass the settings file to the setup_environ() method:
setup_environ(subdir.settings)
Now you should simply be able to import your models and work with them like you were working inside of a Django project:
from django.db import models from subdir.models import Email email_item = Email(email = 'test@example.com', name = 'John Smith', created_at = datetime.datetime.now(), updated_at = datetime.datetime.now()) email_item.save()
This is also especially useful if you need to write scheduled tasks or cronjobs for Django applications and need to import your Django application's environment.
