diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2016-09-30 22:06:02 +0200 |
|---|---|---|
| committer | Markus Holtermann <info@markusholtermann.eu> | 2016-11-25 14:15:49 +0100 |
| commit | cb7bbf97a74fa7800865e3615f196ad65dc4f281 (patch) | |
| tree | 9b22a2a0acdcac0b8ce933349d9738fb08c729f3 /docs/topics/auth/customizing.txt | |
| parent | eb42d8d5d9b0968ce12d09760afff684bee56a3a (diff) | |
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.
Diffstat (limited to 'docs/topics/auth/customizing.txt')
| -rw-r--r-- | docs/topics/auth/customizing.txt | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index 1b9dc6539e..79ffa0a88b 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -487,9 +487,33 @@ different user model. post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL) - Generally speaking, you should reference the user model with the - :setting:`AUTH_USER_MODEL` setting in code that is executed at import - time. ``get_user_model()`` only works once Django has imported all models. + Generally speaking, it's easiest to refer to the user model with the + :setting:`AUTH_USER_MODEL` setting in code that's executed at import time, + however, it's also possible to call ``get_user_model()`` while Django + is importing models, so you could use + ``models.ForeignKey(get_user_model(), ...)``. + + If your app is tested with multiple user models, using + ``@override_settings(AUTH_USER_MODEL=...)`` for example, and you cache the + result of ``get_user_model()`` in a module-level variable, you may need to + listen to the :data:`~django.test.signals.setting_changed` signal to clear + the cache. For example:: + + from django.apps import apps + from django.contrib.auth import get_user_model + from django.core.signals import setting_changed + from django.dispatch import receiver + + @receiver(setting_changed) + def user_model_swapped(**kwargs): + if kwargs['setting'] == 'AUTH_USER_MODEL': + apps.clear_cache() + from myapp import some_module + some_module.UserModel = get_user_model() + + .. versionchanged:: 1.11 + + The ability to call ``get_user_model()`` at import time was added. .. _specifying-custom-user-model: |
