danfairs / django-lazysignup

django-lazysignup is a package designed to allow users to interact with a site as if they were authenticated users, but without signing up. At any time, they can convert their temporary user account to a real user account.
BSD 3-Clause "New" or "Revised" License
410 stars 89 forks source link

Exception in 1.1.2: AppRegistryNotReady: Models aren't loaded yet #57

Closed crgwbr closed 6 years ago

crgwbr commented 7 years ago

The new version 1.1.2 isn't working for me in Django 1.10 or 1.11. The new way to obtain the user model is causing an issue since it gets called during model loading, but requires models to be already loaded.

Traceback (most recent call last):
  File "/foo/sandbox/manage.py", line 15, in <module>
    main()
  File "/foo/sandbox/manage.py", line 11, in main
    execute_from_command_line(sys.argv)
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/lazysignup/models.py", line 27, in <module>
    class LazyUserManager(models.Manager):
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/lazysignup/models.py", line 35, in LazyUserManager
    username_field = get_user_model().USERNAME_FIELD
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 163, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/apps/registry.py", line 192, in get_model
    self.check_models_ready()
  File "/foo/.tox/py36-django111-oscar15/lib/python3.6/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

Reverting back to the LAZYSIGNUP_USER_NAME_FIELD setting would fix this.

LaundroMat commented 6 years ago

Can you try my Django >= 2.0 PR and see if it works?

danfairs commented 6 years ago

I merged @LaundroMat's branch and will focus on support for Django > 2.1.2 only from here on in. (That said, if there's a patch which will make this work for earlier versions of Django without reintroducing the setting, very happy to take a look!). I'll close this for now, but please do reopen if there's a patch you'd like to apply.