Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(providers): Avoid loading Django user models before apps are ready
To register a custom Provider, users subclass `allauth.socialaccount.providers.base.ProviderAccount`. Their subclass is then registered using `allauth.socialaccount.providers.registry.register(MyProvider)`. The registration must happen before the `allauth.socialaccount` app models are ready, as the `SocialApp.provider` choices come from the registry. Otherwise, a Django migration is created because at the time of the import of `allauth.socialaccount.models`, the registry was empty, but when the models are ready (and Django can check migrations) the registry contains the custom provider. Below is an example stack trace to illustrate the issue: ``` File "/home/freitafr/dev/itou/itou/allauth_adapters/peamu/provider.py", line 1, in <module> from allauth.socialaccount.providers.base import ProviderAccount File "/home/freitafr/dev/django-allauth/allauth/socialaccount/providers/base/__init__.py", line 2, in <module> from .provider import Provider, ProviderAccount, ProviderException # noqa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/freitafr/dev/django-allauth/allauth/socialaccount/providers/base/provider.py", line 3, in <module> from allauth.account.models import EmailAddress File "/home/freitafr/dev/django-allauth/allauth/account/models.py", line 12, in <module> from .adapter import get_adapter File "/home/freitafr/dev/django-allauth/allauth/account/adapter.py", line 16, in <module> from django.contrib.auth.models import AbstractUser File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/contrib/auth/models.py", line 3, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/contrib/auth/base_user.py", line 57, in <module> class AbstractBaseUser(models.Model): File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/db/models/base.py", line 129, in __new__ app_config = apps.get_containing_app_config(module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/apps/registry.py", line 260, in get_containing_app_config self.check_apps_ready() File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/apps/registry.py", line 138, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. ```
- Loading branch information