Common Pitfalls - Data Models
If you encounter a strange error, make sure to run django-admin.py validate. That catches some common errors in model syntax.
Using module globals in models
The model module, being a Python module, can import modules or define globals. You might want to use those globals or imported modules in your models classes methods like this:
import md5 class Something(meta.Model): def func(self, parm): md5.new()
Warning
In Django 0.90 and Django 0.91, and in Django trunk up until the merge of the magic-removal branch, you may encounter problems with importing modules or defining global variables in a model file, because those versions of Django create dynamic model classes at runtime which do not get the global scope of the original model file. To work around this, give your model a 'module_constants' dictionary like so:
import md5 class Something(meta.Model): class META: module_constants = { 'md5': md5 } def func(self, parm): md5.new()
Or, simply import the modules needed in the function itself:
class Something(meta.Model): ... def func(self): import md5 md5.new(...)
This problem can also be avoided by using a more recent version of Django (i.e., any trunk version based on the magic-removal codebase).
Symptoms
You get tracebacks with NameErrors in your modules as if the global wasn't there...