For reference, this use-case can be found in django-cms:
[https://github.com/divio/django-
cms/blob/develop/cms/models/permissionmodels.py#L16]
Lets implement an alternative to `get_user_model()` that can be used after
the app registry is populated, but before the user model is loaded. This
can work by actually importing the user model if it's not in the registry
yet:
{{{
from importlib import import_module
from django.apps import apps
from django.conf import settings
from django.utils.module_loading import module_has_submodule
def get_user_model_safe():
"""
Get the user model before it is loaded, but after the app registry is
populated.
"""
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
app_config = apps.get_app_config(app_label)
models_module = import_module("%s.%s" % (app_config.name, "models"))
return getattr(models_module, model_name)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25966>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
I think this is a duplicate of what's described in the comments of #22872,
although the summary and component of that ticket need to be updated as I
assume the original intent of updating the 1.7 release notes with the
backwards-incompatibility won't be done at this point. I'm okay with
closing either ticket as a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:1>
* has_patch: 0 => 1
Comment:
This is implemented as the last commit in
[https://github.com/django/django/pull/6547 PR #6547].
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:2>
* needs_better_patch: 0 => 1
Comment:
I left comments for improvement on the PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:3>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:4>
Comment (by Tim Graham):
Commits from the branch that precede this fix:
* efcb7e1ebf2b852a442d00a31634438359eb4039 - Modified readiness check in
`AppConfig.get_model(s)`.
* fd748c42a96213ebc0a18cc661ecb1abaadabcca - Simplified
`AppConfig.import_models()`.
* 625cd5bcb315154ed28061fefaf639aff54da7c2 - Added `require_ready`
argument to `get_model` methods.
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:6>
* needs_better_patch: 1 => 0
Comment:
I rebased Aymeric's PR onto current master and opened a new PR in
https://github.com/django/django/pull/7550 .
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:7>
* owner: nobody => Markus Holtermann
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:8>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:9>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"cb7bbf97a74fa7800865e3615f196ad65dc4f281" cb7bbf97]:
{{{
#!CommitTicketReference repository=""
revision="cb7bbf97a74fa7800865e3615f196ad65dc4f281"
Fixed #25966 -- Made get_user_model() work at import time.
This makes it equivalent to: `from django.contrib.auth.models import
User`.
Thanks Aymeric Augustin for the initial patch and Tim Graham for the
review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:10>