Search code examples
djangodjango-settingsdjango-endless-pagination

How to pass a callable defined in my django app to a third-party django app via my project settings.py?


I am trying to pass a function defined in my views.py, by specifying a specific setting in settings.py, to a third-party django app used by my project like the following:

ENDLESS_PAGINATION_PAGE_LIST_CALLABLE = myapp.views.get_pagination_pages

and I got the error NameError: name 'lend_borrow' is not defined

Then I tried

import myapp
ENDLESS_PAGINATION_PAGE_LIST_CALLABLE = myapp.views.get_pagination_pages

and another error AttributeError: 'module' object has no attribute 'views'

I also attempted with passing the path to the callable:

ENDLESS_PAGINATION_PAGE_LIST_CALLABLE = 'myapp.views.get_pagination_pages'

But the third-party app doesn't seem to like it. I got Caught TypeError while rendering: 'str' object is not callable

The third-party app being used here is django endless pagination. It's included in INSTALLED_APPS.

How can I pass the callable defined in my app to the third-party app via settings.py?


Solution

  • Clearly, the expectation of the app is the callable and not a string. So, if it works, this should work:

    from myapp.views import get_pagination_pages
    ENDLESS_PAGINATION_PAGE_LIST_CALLABLE = get_pagination_pages
    

    However, you should know that settings is loaded before any of the other files, and if you try to load a views file that imports from models and that in-turn requires settings to be loaded, there can be a circular import and probably the best way to avoid it is by placing the function in a file that doesn't import models, say utils.py or something like that.

    If the function is simple enough, I'd put it in settings itself, or a file parallel to that.