Call Overmortal now at 1 (540) 491-0374 or for more information!

Django Settings Aren't Accessible from Models

We ran into an interesting problem yesterday. As any good programmer knows, you never hard code path information into your code. You use properties files, external XML, etc. In Django, you use the settings.py file.

The settings.py file is a python file like any other. It's all python code. This means that you can add your own stuff in there too - like file and directory paths.

Yesterday we were working on some model code for a client's e-commerce application and the strangest thing happened. It wouldn't let us access the variables from the settings file. Although we imported the settings via "from django.conf import settings" we kept getting a command line error while attempting to validate the model code stating "AttributeError: 'module' object has no attribute [SETTINGS VARIABLE NAME HERE]" and refused to validate.

Of course, we first attempted to check all the spellings and import paths, thinking that we merely had words mixed up or something. When that didn't work, we thought that maybe the models.py file wasn't finding the correct settings file. When that didn't work, we decided to start eliminating code until the error ceased.

The result? It turns out that the following code will break any imports from your settings file:

from tagging.models import *
from tagging.fields import *

In order to get it to work, we had to change it to the following:

from tagging.models import Tag
from tagging.fields import TagField

This, of course, is an importation of classes from the django-tagging module. We were enabling our client to tag her products, as well as categorize them; but this bit was throwing off our models.py file's validation.

The moral of the story is that you really should only import the classes that you need. All developers know this, but sometimes things leak through the cracks - especially when their are so few classes in the imported library. We're posting this here in hopes that anyone else having this issue will be able to find us in the search engines, and save time by avoiding their own investigation.