How do I retrieve a Django model class dynamically?

I think you're looking for this:

from django.db.models.loading import get_model
model = get_model('app_name', 'model_name')

There are other methods, of course, but this is the way I'd handle it if you don't know what models file you need to import into your namespace. (Note there's really no way to safely get a model without first knowing what app it belongs to. Look at the source code to loading.py if you want to test your luck at iterating over all the apps' models.)

Update for Django 1.7+: According to Django's deprecation timeline, django.db.models.loading has been deprecated in Django 1.7 and will be removed in Django 1.9. As pointed out in Alasdair's answer, In Django 1.7+, there is an applications registry. You can use the apps.get_model method to dynamically get a model:

from django.apps import apps
MyModel = apps.get_model('app_label', 'MyModel')

For Django 1.7+, there is an applications registry. You can use the apps.get_model method to dynamically get a model.

from django.apps import apps
MyModel = apps.get_model('app_label', 'MyModel')