views:

24

answers:

2

I've got two applications (app1 and app2) in my django project.

I'm curious if there is a way to import things between applications.

baseProject  
--app1
----models.py  
----etc..
--app2
----models.py
----etc..

I'd like to be able, while in app2, to import something from the models section of app1. Is there an intended method to do this or am I planning bad architecture.

+1  A: 

What you're proposing is fine and accepted practice. From app2, you can simply do: from app1.models import SomeModel. For example, you're probably used to importing the User model from the django.contrib.auth app. This is part of the intended benefit of the reusability of django apps.

ars
preliminary testing appears to suggest this does not work. I'm getting an import error. NOTE: I'm working between two custom apps. Not built int django apps.
Aaron Merriam
@Aaron: That was just an example -- replace `SomeModel` with the name of a model defined in your `app1.models` file. Also, it doesn't matter that it's a custom app, it's the same principle: as long as app1 and app2 are in your path, you should be fine.
ars
@ars yeah. I was just using your syntax to keep things generic. I've been at this for hours and honestly I hadn't done much testing. After inserting the project name into the import statement, everything ran smoothly. Still considering restructuring things.
Aaron Merriam
@Aaron: ah, got it. Glad it worked out.
ars
+3  A: 

You can definitely do that, just import it as usual. Many authentication/registration-related apps import models from the "django.contrib.auth" app that comes with Django. You are free to import from any app, whether you wrote it or not.

You just need to make sure the apps are on your PYTHONPATH, so that they can be imported.

That said, it's always good to consider your design before importing things across apps. Make sure you're not creating a situation where you have a circular dependency between apps.

Andy White
looks like its a PYTHONPATH issue. I think I'm just going to rethink my design. Thanks
Aaron Merriam
Nevermind. Looks like i just overlooked the obvious. Got this working. forgot to add my project name to the import statement.
Aaron Merriam