#29508: Simplify overriding `login_form` in AdminSite
-------------------------------------+-------------------------------------
     Reporter:  Rémi Lapeyre         |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  contrib.admin        |                  Version:  2.0
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Rémi Lapeyre):

 Hi, I made a small example project at
 https://github.com/remilapeyre/bug_29508

 When starting django, we get the following error:

 {{{
 ➜  bug_29508 git:(master) pipenv run bug_29508/manage.py runserver
 Unhandled exception in thread started by <function
 check_errors.<locals>.wrapper at 0x1023eb0d0>
 Traceback (most recent call last):
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/utils/autoreload.py", line 225, in wrapper
     fn(*args, **kwargs)
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/core/management/commands/runserver.py", line 112, in
 inner_run
     autoreload.raise_last_exception()
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/utils/autoreload.py", line 248, in
 raise_last_exception
     raise _exception[1]
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/core/management/__init__.py", line 327, in execute
     autoreload.check_errors(django.setup)()
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/utils/autoreload.py", line 225, in wrapper
     fn(*args, **kwargs)
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/__init__.py", line 24, in setup
     apps.populate(settings.INSTALLED_APPS)
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/apps/registry.py", line 89, in populate
     app_config = AppConfig.create(entry)
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/apps/config.py", line 90, in create
     module = import_module(entry)
   File
 
"/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6/importlib/__init__.py",
 line 126, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
   File "<frozen importlib._bootstrap>", line 955, in
 _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
   File "<frozen importlib._bootstrap>", line 219, in
 _call_with_frames_removed
   File "/Users/remi/home/src/bug_29508/bug_29508/myadmin/__init__.py",
 line 1, in <module>
     from .sites import site
   File "/Users/remi/home/src/bug_29508/bug_29508/myadmin/sites.py", line
 2, in <module>
     from .forms import AdminAuthenticationForm
   File "/Users/remi/home/src/bug_29508/bug_29508/myadmin/forms.py", line
 2, in <module>
     from django.contrib.admin import forms as admin_forms
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/contrib/admin/forms.py", line 2, in <module>
     from django.contrib.auth.forms import AuthenticationForm,
 PasswordChangeForm
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/contrib/auth/forms.py", line 10, in <module>
     from django.contrib.auth.models import User
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/contrib/auth/models.py", line 2, in <module>
     from django.contrib.auth.base_user import AbstractBaseUser,
 BaseUserManager
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/contrib/auth/base_user.py", line 47, in <module>
     class AbstractBaseUser(models.Model):
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/db/models/base.py", line 100, in __new__
     app_config = apps.get_containing_app_config(module)
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/apps/registry.py", line 244, in
 get_containing_app_config
     self.check_apps_ready()
   File
 "/Users/remi/.local/share/virtualenvs/bug_29508-NXp2shP4/lib/python3.6
 /site-packages/django/apps/registry.py", line 127, in check_apps_ready
     raise AppRegistryNotReady("Apps aren't loaded yet.")
 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
 }}}

 The culprit is
 
https://github.com/remilapeyre/bug_29508/blob/master/bug_29508/myadmin/__init__.py#L1
 that will try to load django.contrib.auth.models.User through
 django.contrib.auth.forms.AuthenticationForm to add a placeholder in the
 Authentication form before the apps are registred.

 Of course, we could remove the singleton from there but the customized
 admin would then be less friendly to use than the vanilla one as you could
 not do `myadmin.site.register(MyModel)` like we usually can.

 I'm not sure why this behavior does not happen in the current tests, maybe
 apps are already registred when the override of the settings occurs.

 If there is anything wrong in the example project that I missed, please
 point me to it but it's Django's starter project with just a few
 modifications
 
(https://github.com/remilapeyre/bug_29508/commit/ef75a02aa4eee09062106801502ba8129d8754b1).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29508#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.5bd76ab5a9bdecaae61a002422dadf3c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to