Monday, October 27, 2014

Re: AUTH_USER_MODEL does not accept sub application

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBCAAGBQJUTrA7AAoJEC0ft5FqUuEhN0gQAKdsebpwz5DG7mu+rVQJgP8H
Job8OvlCIyNP71OR91gizm3lvgQl9nJOdSxHNm78a98bFT98DGzCSWp9KVJmbwNk
/zPRQoX4SesT9lNAI0QE7JMZRUffmLXWs2T4nsSW7Rt9CZidfGMUAFVgEXCBgjTj
tqaYN05Pqf1YSr+V0Iv9RA2TImmWMysG6+Ui+q46347vgdCeLlgtX6gNB2858839
bK0z+jlIY9pLv4TEcjTi9UHfD6QaVrixNFZQBNP+u6H/QeUVm5YlsAiF14wRTN7K
36F+Q1UPYp+NmR5D6uCKjINZWzzmYKK+7V0OyM6YuPm2QDXyiun8SuOLbTlu4pDD
VPTRMbVyV9vGPJHgdv8UR1KoxlePE80mLuQ3RDvRyrDPsNtNLdos765ifkSTy42Z
T+6RoQwP68xy5vs741CglLhIxa+kMHcHKzkR1dYSDYVFu1ePPoRE1F8b6QVjLMaG
VPNpFHwJcQCdJFZli/w0AvJL6Nrg+Biy3s/Rfe+ENLL8va0blZe8Wt7Q/D7NFTd5
8fPwP9rTQlpvGv3qmjOS8/7KxKSSc6vVNwOLbjOBEhIzfP5i7eUitRdS8E81XhKW
zdIvngrMYA3R42+BZ29N0T+4rlznQgpR7as+sp3b70ighh8PcJAjqZ3Q2jcby9p9
n5hyjV9l6taejSWujN/W
=waSa
-----END PGP SIGNATURE-----
Hi Frankline,

On 10/27/2014 12:38 AM, Frankline wrote:
> I am having problems with my custom user model while using Django 1.7.1
> and Python 3.4.
> I have declared a Custom user model in an apps.users.AuthUser. I then
> have another application (apps.pets) that will use the AuthUser as a
> ForeignKey in a Pet model. See below:
>
> class Pet(models.Model):
> owner = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True,
> blank=False, null=False)
>
> This is my scenario:
>
> In my INSTALLED_APPS I have: "apps.users"
>
> If I set AUTH_USER_MODEL="apps.users.AuthUser" the exception is raised
> when I run 'runserver':
> File
> "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/contrib/auth/checks.py",
> line 12, in check_user_model
> cls = apps.get_model(settings.AUTH_USER_MODEL)
> File
> "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/apps/registry.py",
> line 201, in get_model
> app_label, model_name = app_label.split('.')
> ValueError: too many values to unpack (expected 2)
>
>
> If I set AUTH_USER_MODEL="users.AuthUser" the exception is raised when I
> run migrate:
> File
> "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/db/migrations/state.py",
> line 89, in render
> model=lookup_model,
> ValueError: Lookup failed for model referenced by field pets.Pet.owner:
> users.AuthUser
>
> Similar issue reported here I guess:
> https://code.djangoproject.com/ticket/19845
>
>
> Does this mean that in AUTH_USR_MODEL setting I have to use
> 'app_label.model_name' instead of 'apps.app_label.model_name.'? Is there
> a workaround for this?

Why is a workaround needed?

Django has the concept of "app label", which is usually the final
component of the app's import path (although in Django 1.7 with
AppConfig it no longer has to be). So for an app whose import path is
"apps.users", its app label is "users". And the format of
AUTH_USER_MODEL (like the format for string model references elsewhere
in Django) is simple "app_label.ModelName", not a full Python import path.

So having "apps.users" in INSTALLED_APPS and "users.User" in
AUTH_USER_MODEL is fully correct and normal; there is no bug there, and
no need for a workaround.

(That said, I do find the whole concept of app-label unfortunate -- why
not just use full import paths and avoid conflicts between "foo.users"
and "bar.users"? -- and especially the use of dotted syntax in
"app_label.ModelName", which looks so much like an import path. But
nevertheless, app labels are a long-standing part of Django legacy which
can't be easily excised.)

Carl

No comments:

Post a Comment