动态调用方法(如果 Django 中存在)
我正在创建基于Django的网站(我知道它是纯Python,所以也许熟悉Python的人也可以回答),我需要动态调用一些方法。
例如,我的网站中很少有应用程序(模块)在views.py 中使用“do_search()”方法。然后我有一个名为“search”的模块,我想要一个能够调用其他应用程序中所有现有“do_search()”的操作。当然我不喜欢将每个应用程序都添加到导入中,然后直接调用。我需要一些更好的方法来动态地完成它。
我可以从设置中读取 INSTALLED_APPS 变量,并以某种方式运行所有已安装的应用程序并查找特定方法?一段代码在这里会有很大帮助:)
提前致谢! 伊格纳斯
I'm creating website based on Django (I know it's pure Python, so maybe it could be also answered by people who knows Python well) and I need to call some methods dynamically.
For example I have few applications (modules) in my website with the method "do_search()" in the views.py. Then I have one module called for example "search" and there I want to have an action which will be able to call all the existing "do_search()" in other applications. Of course I don't like to add each application to the import, then call it directly. I need some better way to do it dynamically.
I can read INSTALLED_APPS variable from settings and somehow run through all of the installed apps and look for the specific method? Piece of code will help here a lot :)
Thanks in advance!
Ignas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否真正理解这个问题,但如果我不在,请在对我的答案的评论中澄清。
至于在哪里注册您的搜索引擎,django 的信号文档中有一些与此相关的有用文档。
因此,您的
models.py
文件应该是注册搜索引擎的好地方。我刚刚想到的替代答案:
在您的
settings.py
中,您可以有一个声明所有搜索功能的设置。就像这样:这有点类似于注册 MIDDLEWARE_CLASSES 和 TEMPLATE_CONTEXT_PROCESSORS。上面都是未经测试的代码,但是如果你查看 django 源代码,你应该能够充实它并消除任何错误。
I'm not sure if I truly understand the question, but please clarify in a comment to my answer if I'm off.
As for where to register your search engine, there is some sort of helpful documentation in the signals docs for django which relates to this.
So your
models.py
file should be a good place to register your search engine.Alternative answer that I just thought of:
In your
settings.py
, you can have a setting that declares all your search functions. Like so:This works somewhat similar to registering MIDDLEWARE_CLASSES and TEMPLATE_CONTEXT_PROCESSORS. The above is all untested code, but if you look around the django source, you should be able to flesh this out and remove any errors.
如果您可以通过导入其他应用程序
,那么应该可以执行
但是您的方法是有问题的。
If you can import the other applications through
then it should be possible to perform
However your approach is questionable.