Hey guys, I'm using Python Django SDK with passwor...
# support-questions
Hey guys, I'm using Python Django SDK with passwordless recipe and I ran into a problem with
. I'll start a thread and explain the details
Copy code
from typing import Union, Dict, Any

from supertokens_python.recipe.session.interfaces import RecipeInterface

from apps.profile.models.role import ProfileRole

def override_functions(original_implementation: RecipeInterface):
    original_implementation_create_new_session = original_implementation.create_new_session

    async def create_new_session(request: Any, user_id: str,
                                 access_token_payload: Union[None, Dict[str, Any]],
                                 session_data: Union[None, Dict[str, Any]], user_context: Dict[str, Any]):
        if session_data is None:
            session_data = {}

        if access_token_payload is None:
            access_token_payload = {}

        access_token_payload["roles"] = {}
        profile_roles = ProfileRole.objects.filter(profile__supertokens_user_id=user_id)
        for profile_role in profile_roles:
            if profile_role.at not in access_token_payload["roles"]:
                access_token_payload["roles"][profile_role.at] = [profile_role.role]

        return await original_implementation_create_new_session(request, user_id, access_token_payload,
                                                                session_data, user_context)

    original_implementation.create_new_session = create_new_session
    return original_implementation
So I have this override function which references a model from another application with roles
The problem with this function is that I have to initialise supertokens in the core settings module, where I have to call
to set correct values into
. But I cannot call that function before I initialise supertokens with my override function. But if I try to do that, Django complains that I am trying to import a model before django apps are initialized
So here is the theoretically correct flow: 1. Initialise supertokens with my custom override function 2. Set CORS_ALLOW_HEADERS in the Django settings 3. Django initialises apps after core settings module is initialised 4. Enjoy 🙂 The problem is that step-3 is required for step-1, because my custom override function uses a model from an app. And step-1 required for step-2, because I have to initialise supertokens in order to call
Any ideas what can I do? 😅
The only solution that I can see here is to hardcode the return values of
instead of calling this function in the settings module 🤔 Do you think it is a good idea?
I found a solution, nevermind 😅 😅 This has solved my problem: https://docs.djangoproject.com/en/4.0/ref/applications/#django.apps.AppConfig.ready
Here is my solution btw:
Copy code
from django.apps import apps
ProfileRole = apps.get_model('profile', 'ProfileRole')