Piper Merriam
Piper Merriam

Reputation: 2954

Importing between two applications in a django project

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.

Upvotes: 3

Views: 3820

Answers (2)

Andy White
Andy White

Reputation: 88345

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.

Upvotes: 6

ars
ars

Reputation: 123468

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.

Upvotes: 1

Related Questions