[Django] #25966: Implement get_user_model() that can be used in models.py

67 views
Skip to first unread message

Django

unread,
Dec 22, 2015, 9:41:56 AM12/22/15
to django-...@googlegroups.com
#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.

Django

unread,
Dec 22, 2015, 10:44:30 AM12/22/15
to django-...@googlegroups.com
#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 | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Oct 5, 2016, 8:26:41 PM10/5/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Oct 6, 2016, 7:49:28 PM10/6/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


Comment:

I left comments for improvement on the PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:3>

Django

unread,
Oct 26, 2016, 11:27:49 AM10/26/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:4>

Django

unread,
Oct 28, 2016, 7:48:16 PM10/28/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

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>

Django

unread,
Oct 31, 2016, 2:38:10 PM10/31/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:6>

Django

unread,
Nov 13, 2016, 9:44:44 AM11/13/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
--------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody

Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Markus Holtermann):

* 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>

Django

unread,
Nov 13, 2016, 9:44:53 AM11/13/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: Markus
| Holtermann
Type: New feature | Status: assigned
Component: contrib.auth | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markus Holtermann):

* owner: nobody => Markus Holtermann
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:8>

Django

unread,
Nov 15, 2016, 8:34:25 AM11/15/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: Markus
| Holtermann
Type: New feature | Status: assigned
Component: contrib.auth | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25966#comment:9>

Django

unread,
Nov 23, 2016, 11:57:14 AM11/23/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: Markus
| Holtermann
Type: New feature | Status: assigned
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin

Django

unread,
Nov 25, 2016, 8:34:35 AM11/25/16
to django-...@googlegroups.com
#25966: Implement get_user_model() that can be used in models.py
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: Markus
| Holtermann
Type: New feature | Status: closed
Component: contrib.auth | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markus Holtermann <info@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages