开发者

Importing between two applications in a django project

I've got two applications (app1 and app2) in my django projec开发者_如何学Got.

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜