#25890 closed Cleanup/optimization (invalid)
AUTH_USER_MODEL unfriendly to custom models with more than one level of encapsulation
| Reported by: | Junhua LIU | Owned by: | nobody |
|---|---|---|---|
| Component: | contrib.auth | Version: | 1.9 |
| Severity: | Normal | Keywords: | AUTH_USER_MODEL |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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
Change History (3)
follow-up: 2 comment:1 by , 10 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:2 by , 10 years ago
How about a custom User Model that implement AbstractBaseUser, and is located at apps/authn/CustomUser?
Replying to aaugustin:
An application label cannot contain periods.
For instance the
Usermodel of thedjango.contrib.authapp is located atdjango.contrib.auth.models.User. The application's name isdjango.contrib.authand its label isauth. The default value ofAUTH_USER_MODELisauth.User.
comment:3 by , 10 years ago
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.
An application label cannot contain periods.
For instance the
Usermodel of thedjango.contrib.authapp is located atdjango.contrib.auth.models.User. The application's name isdjango.contrib.authand its label isauth. The default value ofAUTH_USER_MODELisauth.User.