pydanny/dj-stripe — pull request #105 by shvechikov (2014-08-20 12:46:52+00:00)
Title: Fix AppRegistryNotReady exception at import time

This commit fixes an issue with loading djstripe app (after adding
'djstripe' to settings.INSTALLED_APPS) in my django project
(Django 1.7).

The problem was with using django.contrib.auth.get_user_model() inside
djstripe.settings module at import time which is not recommened
practice:

> Another common culprit is django.contrib.auth.get_user_model().
> Use the AUTH_USER_MODEL setting to reference the User model
> at import time.

https://docs.djangoproject.com/en/1.7/ref/applications/#troubleshooting

Fixed by using SimpleLazyObject.

The original traceback:

Traceback (most recent call last):
File ".../my_project/manage.py", line 10, in
execute_from_command_line(sys.argv)
File ".../site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File ".../site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File ".../site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File ".../site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File ".../site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File ".../python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File ".../site-packages/djstripe/models.py", line 24, in
from .settings import PAYMENTS_PLANS, INVOICE_FROM_EMAIL, SEND_INVOICE_RECEIPT_EMAILS
File ".../site-packages/djstripe/settings.py", line 22, in
User = get_user_model()
File ".../site-packages/djstripe/settings.py", line 17, in get_user_model
User = get_user_model()
File ".../site-packages/django/contrib/auth/__init__.py", line 136, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL)
File ".../site-packages/django/apps/registry.py", line 199, in get_model
self.check_models_ready()
File ".../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.