[Django] #25890: AUTH_USER_MODEL unfriendly to custom models with more than one level of encapsulation

2 views
Skip to first unread message

Django

unread,
Dec 8, 2015, 3:48:53 AM12/8/15
to django-...@googlegroups.com
#25890: AUTH_USER_MODEL unfriendly to custom models with more than one level of
encapsulation
--------------------------------------+-----------------------------
Reporter: junhua | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 1.9
Severity: Normal | Keywords: AUTH_USER_MODEL
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+-----------------------------
In db/models/utils.py, when model is a string, it's handled by the
following:

`app_label, model_name = model.split(".")`

In the event if AUTH_USER_MODEL has more than a level of encapsulation
(e.g. "apps/authn/CustomUser"), it throws a ValueError: too many values to
unpack.

Suggested fix:

{{{
def make_model_tuple(model):
"""
Takes a model or a string of the form "app_label.ModelName" and
returns a
corresponding ("app_label", "modelname") tuple. If a tuple is passed
in,
it's assumed to be a valid model tuple already and returned unchanged.
"""
if isinstance(model, tuple):
model_tuple = model
elif isinstance(model, six.string_types):
strings = model.split(".")
app_label = ".".join(strings[:-1])
model_name = strings[-1]
# app_label, model_name = model.split(".")
model_tuple = app_label, model_name.lower()
else:
model_tuple = model._meta.app_label, model._meta.model_name
assert len(model_tuple) == 2, "Invalid model representation: %s" %
model
return model_tuple
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25890>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 8, 2015, 4:21:31 AM12/8/15
to django-...@googlegroups.com
#25890: AUTH_USER_MODEL unfriendly to custom models with more than one level of
encapsulation
-------------------------------------+-------------------------------------
Reporter: junhua | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution: invalid

Keywords: AUTH_USER_MODEL | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

An application label cannot contain periods.

For instance the `User` model of the `django.contrib.auth` app is located
at `django.contrib.auth.models.User`. The application's name is
`django.contrib.auth` and its label is `auth`. The default value of
`AUTH_USER_MODEL` is `auth.User`.

--
Ticket URL: <https://code.djangoproject.com/ticket/25890#comment:1>

Django

unread,
Dec 8, 2015, 5:16:38 AM12/8/15
to django-...@googlegroups.com
#25890: AUTH_USER_MODEL unfriendly to custom models with more than one level of
encapsulation
-------------------------------------+-------------------------------------
Reporter: junhua | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution: invalid

Keywords: AUTH_USER_MODEL | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by junhua):

How about a custom User Model that implement AbstractBaseUser, and is
located at apps/authn/CustomUser?

Replying to [comment:1 aaugustin]:


> An application label cannot contain periods.
>
> For instance the `User` model of the `django.contrib.auth` app is
located at `django.contrib.auth.models.User`. The application's name is
`django.contrib.auth` and its label is `auth`. The default value of
`AUTH_USER_MODEL` is `auth.User`.

--
Ticket URL: <https://code.djangoproject.com/ticket/25890#comment:2>

Django

unread,
Dec 8, 2015, 12:16:52 PM12/8/15
to django-...@googlegroups.com
#25890: AUTH_USER_MODEL unfriendly to custom models with more than one level of
encapsulation
-------------------------------------+-------------------------------------
Reporter: junhua | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution: invalid

Keywords: AUTH_USER_MODEL | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

I have no idea what `apps/authn/CustomUser` means. If your file doesn't
have at least a `.py` extension nothing's going to work.

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

Reply all
Reply to author
Forward
0 new messages