Opened 2 years ago

Closed 3 weeks ago

#22872 closed Cleanup/optimization (wontfix)

Backwards incompatible change: Can't proxy User model: RuntimeError: App registry isn't ready yet.

Reported by: Jon Dufresne Owned by: nobody
Component: Documentation Version: 1.7
Severity: Normal Keywords:
Cc: app-loading Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


My application creates proxies of the user model to add custom methods that are useful in certain contexts. There is an attempt to do this in a AUTH_USER_MODEL agnostic way. This fails with the latest beta version of 1.7.

Starting from a fresh install and project I created the following files in directory myapp.


from django.contrib.auth import get_user_model

class MyUser(get_user_model()):
    def my_method(self):
        return 'foo'

    class Meta:
        proxy = True


from django.test import TestCase
from myapp.models import MyUser

class MyTestCase(TestCase):
    def test_my_user(self):
        user = MyUser()

Django 1.7 beta 2

$ python test
Traceback (most recent call last):
  File "", line 10, in <module>
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/core/management/", line 427, in execute_from_command_line
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/core/management/", line 391, in execute
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/", line 21, in setup
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/apps/", line 106, in populate
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/apps/", line 190, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/", line 37, in import_module
  File "/home/jon/djtest/djtest/myapp/", line 4, in <module>
    class MyUser(get_user_model()):
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/contrib/auth/", line 136, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/apps/", line 187, in get_model
  File "/home/jon/djtest/venv/lib/python2.7/site-packages/django/apps/", line 119, in check_ready
    raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.

Django 1.6.5

$ python test
Creating test database for alias 'default'...
Ran 1 test in 0.001s

Destroying test database for alias 'default'...

If there is a better (or forward compatible) way proxy an unknown User model, I'd be happy to hear about it.

Change History (14)

comment:1 Changed 2 years ago by Aymeric Augustin

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

As part of the app-loading refactor, it's expected that calling get_user_model() at compile-time fail with this message.

The best idea I have is to create an AppConfig for your app and create the model in its ready() method, then add it to the module.

I suspect that many non-declarative models will require this pattern.

I'm leaving the ticket open for now in case someone has other ideas which may involve changes in Django.

comment:2 in reply to:  1 ; Changed 2 years ago by Jon Dufresne

Replying to aaugustin:

The best idea I have is to create an AppConfig for your app and create the model in its ready() method, then add it to the module.

How would one go about importing this model for use elsewhere (other models, forms, views, etc.)? These references are often used at compile time such as model forms and generic views. It seems, importing the proxy model would never work at compile time. Every function that referenced the proxy model directly would need to import it at the top of the function so as to delay the import. Or, I guess, implement another get_user_model() style function and sprinkle it all over.

comment:3 in reply to:  2 Changed 2 years ago by Aymeric Augustin

Indeed, my suggestion doesn't work.

Replying to jdufresne:

Or, I guess, implement another get_user_model() style function and sprinkle it all over.

I'm not sure that helps, because you still need to resolve the model at compile time.

The only thing that works at compile time currently is settings.AUTH_USER_MODEL. It can be used a an FK target because Django specifically delays resolution of FKs until the target model is imported (search for class_prepared in the code).

comment:4 Changed 2 years ago by Tim Graham

Cc: app-loading added
Component: UncategorizedDocumentation
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I guess we can accept this as a documentation improvement?

comment:5 Changed 2 years ago by Phillip Marshall

I also have an app that uses a user proxy (also with class MyUser(get_user_model()):), which i'm porting up to 1.7. Looking through the docs it seemed like using apps.get_model('app.model') (equivalent to apps.get_model('app', 'model')) was they way to go about proxying to the user model:

from django.apps import apps
from django.conf import settings

class MyUser(apps.get_model(settings.AUTH_USER_MODEL)):
    class Meta:
        proxy = True

but this raises "django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet."

It looks like this can be worked around by using the longer apps.get_app_config('app').get_model('model'). I'm not sure if this is intended behavior or a bug:

user_app, user_model = settings.AUTH_USER_MODEL.split('.')

class MyUser(apps.get_app_config(user_app).get_model(user_model)):

Some caveats: this only works if your model is after the base user model in INSTALLED_APPS, and as long as AUTH_USER_MODEL only has one dot.

I suppose circumvents the whole point of the new app system, so that plus the caveats probably makes it a bad idea to use. Rather than the above, I think a new get_user_model() should be made, or a more clear way of doing this should be documented.

The above stuff was tested on 1.7c2 and the latest stable/1.7.x as of writing, - I also haven't checked any of the implications of this with migrations.


comment:6 Changed 23 months ago by Tim Graham

Version: 1.7-beta-21.7

comment:7 Changed 21 months ago by Tim Graham

I noticed some ugly code in django-cms to work around this issue. That code is currently inspecting apps.all_models to get the user model before the apps registry is ready.

It seems to me as long as the application declaring the custom user model appears in INSTALLED_APPS before the application that calls get_user_model() there shouldn't be a problem with this technique. Am I wrong?

comment:8 Changed 21 months ago by Aymeric Augustin

During the app-loading refactor, we agreed that we didn't want to introduce constraints on the ordering of INSTALLED_APPS other than precedence for finding templates, translations, etc. Otherwise you could very quickly end up with a set of incompatible contraints.

I'm -1 on making the ability to import models conditional on a properly ordered INSTALLED_APPS. Debugging import loops during models loading is bad enough already (albeit better since 1.7).

If you want to get the user model anyway, apps.all_models or apps.get_registered_model both "work", in the sense that they'll find the model if it was already registered. The latter looks more like an API than the former but there's a ticket about removing it.

comment:9 Changed 21 months ago by Aymeric Augustin

One solution may be to introduce the equivalent of django-oscar's get_model() function and change get_user_model() to use it.

I don't think it reintroduces any of the problems app-loading eliminated, although that's hard to prove.

comment:10 Changed 13 months ago by Simon Charette

The introduction of an equivalent shim look reasonable. Do you have an API in mind?

comment:11 Changed 11 months ago by Ivan Ven Osdel

I feel like this breaking change should have been added to the Django 1.7, 1.8 and 1.9 breaking changes section. Is it too late for that?

comment:12 Changed 11 months ago by Tim Graham

No, feel free to submit a patch. It would only be added to the 1.7 release notes, not all subsequent versions.

comment:13 in reply to:  12 Changed 11 months ago by Ivan Ven Osdel

Replying to timgraham:

No, feel free to submit a patch. It would only be added to the 1.7 release notes, not all subsequent versions.

Nevermind on me updating the docs. I have run into issues with app loading and model inheritence but I am not sure they are related to this issue and they need to be better pinned down before I can do a proper docs update.

Last edited 11 months ago by Ivan Ven Osdel (previous) (diff)

comment:14 Changed 3 weeks ago by Tim Graham

Resolution: wontfix
Status: newclosed

Closing since 1.7 is old and no one has bothered to write a patch for the release notes. #25966 is for allowing using get_user_model() at the module level.

Note: See TracTickets for help on using tickets.
Back to Top