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
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.pyfile 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.