Importing models from outside applications

771 views
Skip to first unread message

johnnie pittman

unread,
Jan 9, 2007, 7:58:40 PM1/9/07
to django...@googlegroups.com
I've done a good amount of searching the archives and the wiki on the site looking for the answer to this question.  If this has already been answered, please accept my apology for repeating the question and please point me to the answer.

I'm looking to find out if there's a method to access django models in other applications outside of the django project structure.   What I'm looking to do is import a model from one of the apps in my project:

from project.app import models

into a seperate application that I've written that does not use django otherwise.  This other app is a data collection system, collecting the data and dropping it into the db for the django app.  Both applications are accessing the same tables and I'd like to not have to write new code in one app (sql) when I can use the django models api if possible.

Any pointers on this would be great. 

Thanks
Johnnie

Russell Keith-Magee

unread,
Jan 10, 2007, 8:29:25 PM1/10/07
to django...@googlegroups.com
On 1/10/07, johnnie pittman <johnnie...@gmail.com> wrote:

> I'm looking to find out if there's a method to access django models in other
> applications outside of the django project structure. What I'm looking to
> do is import a model from one of the apps in my project:
>
> from project.app import models

Have you tried doing exactly that? A Django app is just a python
module, so as long as project.app is on your PYTHONPATH when you run
manage.py, a simple import should work.

Yours,
Russ Magee %-)

Waylan Limberg

unread,
Jan 10, 2007, 8:56:25 PM1/10/07
to django...@googlegroups.com
Of course, if your not running your app through manage.py, you'll need
to define the DJANGO_SETTINGS_MODULE environment variable. After all,
for you model to connect with a db, it'll need the connection
settings.

A minimal settings file with db connection info should do the trick.
Depending on how you plan to run your app, you can either permenantly
set the DJANGO_SETTINGS_MODULE environment variable via the OS or in
you python code.

In your python code this should do the trick ( where myproject is on
your pythonpath):

import os
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"


--
----
Waylan Limberg
way...@gmail.com

johnnie pittman

unread,
Jan 10, 2007, 9:17:56 PM1/10/07
to django...@googlegroups.com
Hey Russ and Waylan,

Thanks for the responses.  

Russ, I actually did that:

import sys
sys.path.append("path/to/project")
from project.app import models

complained about environment settings.  But when I dug into fixing that, I over complicated the matter.  Waylan's response saw through my over complication and we get to:

import os, sys
sys.path.append("path/to/project")
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"

from project.app import models

and that works great.

Again, thank you both for your response.

Johnnie
Reply all
Reply to author
Forward
0 new messages