开发者

How can I get Django application name for a file inside that application?

Having full path for the file that is a part of django application I would like to get a Django application name.

For example for this path:

/lib/python2.6/site-packages/django/contrib/auth/tests/auth_backends.py

Application name is auth.开发者_如何学编程

I wonder how this Django application name could be programatically calculated for specific filename inside an app.

Background: I want to integrate calling Django test management command from a vim editor. It should run tests for an app currently edited file belongs.


You can get module by filename with __import__. You can get it's package name with __package__ attribute. Then you can check which app from settings.INSTALLED_APPS is a substring of package name. It's your app.


If you have an instance of a class in that module you could try:

instance._meta.app_label
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜