Django:如何处理可重用的应用程序中的导入 [英] Django: How to handle imports in a reusable app

查看:90
本文介绍了Django:如何处理可重用的应用程序中的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆