Search code examples
pythondjangotornado

How can I use the Django ORM in my Tornado application?


I have an existing Django application with a database and corresponding models.py file.

I have a new Tornado application that provides a web service to other applications. It needs to read/write from that same database, and there is code in the models file I'd like to use.

How can I best use the Django database and models in my Tornado request handlers? Is it as simple as making a symbolic link to the models.py Django project folder, importing Django modules, and using it? I guess I'd have to do settings.configure(), right?

Thanks!


Solution

  • Add the path to the Django project to the Tornado application's PYTHONPATH env-var and set DJANGO_SETTINGS_MODULE appropriately. You should then be able to import your models and use then as normal with Django taking care of initial setup on the first import.

    You shouldn't require any symlinks.