﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25966	Implement get_user_model() that can be used in models.py	Marten Kenbeek	Markus Holtermann	"`get_user_model()` can only be used when the models for the user app have been loaded into the registry. This can be problematic when you need to subclass the user model, and you need the actual model in your own models.py file. If you want to support swappable models, your app must be after the user app in `INSTALLED_APPS`. 

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)
}}}"	New feature	closed	contrib.auth	dev	Normal	fixed		rene@…	Ready for checkin	1	0	0	0	0	0
