Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:27:21+00:00 2026-05-20T09:27:21+00:00

I’m creating website based on Django (I know it’s pure Python, so maybe it

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:27:22+00:00Added an answer on May 20, 2026 at 9:27 am

    I’m not sure if I truly understand the question, but please clarify in a comment to my answer if I’m off.

    # search.py
    searchables = []
    
    def search(search_string):
        return [s.do_search(search_string) for s in searchables]
    
    def register_search_engine(searchable):
        if hasattr(searchable, 'do_search'):
            # you want to see if this is callable also
            searchables.append(searchable)
        else:
            # raise some error perhaps
    
    
    # views.py
    def do_search(search_string):
        # search somehow, and return result
    
    # models.py
    
    # you need to ensure this method runs before any attempt at searching can begin
    # like in models.py if this app is within installed_apps. the reason being that
    # this module may not have been imported before the call to search.
    import search
    from views import do_search
    search.register_search_engine(do_search)
    

    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.

    You can put signal handling and registration code anywhere you like. However, you’ll need to make sure that the module it’s in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app’s models.py a good place to put registration of signal handlers.

    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:

    # settings.py
    SEARCH_ENGINES = ('app1.views.do_search', 'app2.views.do_search')
    
    # search.py
    from django.conf import settings
    from django.utils import importlib
    
    def search(search_string):
        search_results = []
        for engine in settings.SEARCH_ENGINES
           i = engine.rfind('.')
           module, attr = engine[:i], engine[i+1:]
           mod = importlib.import_module(module)
           do_search = getattr(mod, attr)
           search_results.append(do_search(search_string))
        return search_results
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.