PyCharm 自动导入忽略 Django 项目名称
PyCharm 允许您自动导入未找到的类...因此,例如,如果我键入当前文件中未找到的方法名称或类名称,PyCharm 可以自动为其添加导入语句。但是,问题是自动导入会丢失当前的 Django 项目名称...
例如:
您的 Django 项目名为 myproject,当前应用程序为 myapp 。您的 models.py 中有一个名为 MyModel 的类。
如果您在某处输入“MyModel”,自动生成的导入语句将是:
from myapp.models import MyModel
但是我希望它是:
from myproject.myapp.models import MyModel
有谁知道如何解决这个问题吗?
PyCharm allows you to auto-import a class that is not found... So for instance if I typed a method name or class name, that is not found in the current file, PyCharm can automatically add an import statement for it. However, the problem is that the auto-import misses the current Django projects name...
So for example:
Your Django project is called myproject, and the current application is myapp. You have a class in your models.py called MyModel.
If you type in "MyModel" somewhere, the automatically generated import statement will be:
from myapp.models import MyModel
I however want it to be:
from myproject.myapp.models import MyModel
Does anyone have any idea how this can be fixed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将项目名称包含在导入中是一种不好的做法。重用您的应用程序变得更加困难。如果您愿意,您无法轻易更改项目名称。一些主机(例如 ep.io)甚至要求在导入中不使用项目名称。而且据我所知,您无法在 PyCharm 中更改此类导入行为。您可以尝试在他们的 bugtracker 上填写请求,但我不确定它是否会被实施。
It`s a bad practice to include project name into imports. It makes harder to reuse your app. And you cannot easily change project name if you want to. Some hostings like ep.io even requires to not use a project name in imports. And afaik you cannot change such import behavoiur in PyCharm. You can try to fill a request at their bugtracker, but i doudt that it will be implemented.