#25966: Implement get_user_model() that can be used in models.py
------------------------------+--------------------
     Reporter:  knbk          |      Owner:  nobody
         Type:  New feature   |     Status:  new
    Component:  contrib.auth  |    Version:  master
     Severity:  Normal        |   Keywords:
 Triage Stage:  Unreviewed    |  Has patch:  0
Easy pickings:  0             |      UI/UX:  0
------------------------------+--------------------
 `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)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25966>
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/047.78475bd923c1949a333aafa0b3c1c3b1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to