Django:如何处理可重用的应用程序中的导入 [英] Django: How to handle imports in a reusable app
本文介绍了Django:如何处理可重用的应用程序中的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
view.py
from project.app.models导入MyModel
所以,如果我将我的应用移动到另一个项目,我必须修改 project.app.models,这样它就像我所移动的每个应用程序都是project2.app.models。
有没有办法避免这种情况?
提前感谢
解决方案
您应该使用包含应用程序的目录更新您的Python路径。
这样你只需要:
from app.models导入MyModel
I'm just starting with django. It is not quite clear to me, how should I write an app I could reuse later. In every tutorial I read I see the same piece of code:
view.py
from project.app.models import MyModel
So, if I move my apps to another project, I'll have to modify the "project.app.models" so that it looks like "project2.app.models" for every app I move. Is there a way to avoid that?
Thanks in advance.
解决方案
You should update your Python path with the directory containing your apps.
This way you just have to :
from app.models import MyModel
这篇关于Django:如何处理可重用的应用程序中的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文