Django 1.8: subclass AbstractUser, App 'auth' doesn't have a 'myuser' model.

1,100 views
Skip to first unread message

술욱

unread,
Oct 6, 2015, 9:38:23 PM10/6/15
to 'Tom
Hello,

I'm trying to upgrade an app from Django 1.6 to 1.8 but I can't solve
a problem apparently related to a custom user model.

The project has an 'auth' app with its corresponding models.py and
apps.py. The snippet pasted bellow reproduces the problem which I
don't know how to fix.

Many TIA,
Norberto


# auth/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models

class MyUser(AbstractUser):
pass


# auth/apps.py
from django.apps import AppConfig

class MyAuth(AppConfig):
name = 'auth'
label = 'myauth'


# project/setting.py

INSTALLED_APPS = (
...
'auth.apps.MyAuth',
...
)

AUTH_USER_MODEL = 'auth.MyUser'


Note apps.py is there just because 'auth' collides with django.contrib.auth.


$ ./manage.py makemigrations
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/apps/config.py",
line 159, in get_model
return self.models[model_name.lower()]
KeyError: 'myuser'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/management/__init__.py",
line 351, in execute_from_command_line
utility.execute()
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/management/__init__.py",
line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/management/base.py",
line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/management/base.py",
line 444, in execute
self.check()
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/management/base.py",
line 482, in check
include_deployment_checks=include_deployment_checks,
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/core/checks/registry.py",
line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/ubuntu/.virtualenvs/testauth/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/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/apps/registry.py",
line 202, in get_model
return self.get_app_config(app_label).get_model(model_name.lower())
File "/home/ubuntu/.virtualenvs/testauth/lib/python3.4/site-packages/django/apps/config.py",
line 162, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'auth' doesn't have a 'myuser' model.

술욱

unread,
Oct 13, 2015, 4:31:03 PM10/13/15
to django...@googlegroups.com
Hello,

I'm re-posting this.

Note that I already tried AUTH_USER_MODEL = "myauth.MyUser', and while
that makes the problem disappear, the change also creates new tables
in the database.

What are my options? AFAIK 1.6 is deprecated for security reasons and
I want to upgrade to 1.8.x.

Thanks.
Norberto

Tim Graham

unread,
Oct 13, 2015, 5:26:42 PM10/13/15
to Django users
Do you want to use the custom user model or not? What extra tables are created?

술욱

unread,
Oct 14, 2015, 11:44:39 AM10/14/15
to django...@googlegroups.com
Hello Tim,

yes, I need to use the custom user model. The app is now in production
(Django 1.6.11).

If I use settings.AUTH_USER_MODEL = 'auth.MyUser', I get the error
described in my previous emails.

If I change to settings.AUTH_USER_MODEL = 'myauth.MyUser', I get the
following tables created:

myauth_myuser
myauth_myuser_groups
myauth_myuser_user_permissions

Thanks,
Norberto
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7f285c9a-6088-4615-8b68-342322ad89c7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

syl pingus

unread,
Oct 14, 2015, 2:23:28 PM10/14/15
to django...@googlegroups.com
Hello Norberto,

In your first example, I think the name of your apps in the apps.py config file conflicts with the core Django auth module and it could explains the second exception you get.

When you use a custom model in Django, the "migrate" command always create additional table for group and user_permissions but they are never filled in throught the admin site. According to me, Django creates these tables but they are useless. Why ? I don't really know... Maybe it's depending on the table creation process.

If you want to use group and permissions with your custom model, it will be stored in auth_group, auth_group_permissions and auth_permissions tables.

Sorry, my answer doesn't really help you but that's is my understanding of custom user model process at this time.

Sylvain.
--
> From: nbe...@gmail.com
> Date: Wed, 14 Oct 2015 12:43:03 -0300
> Subject: Re: Django 1.8: subclass AbstractUser, App 'auth' doesn't have a 'myuser' model.
> To: django...@googlegroups.com

술욱

unread,
Oct 14, 2015, 10:15:16 PM10/14/15
to django...@googlegroups.com
2015-10-14 15:21 GMT-03:00 syl pingus <sylp...@hotmail.fr>:
> Hello Norberto,
>
> In your first example, I think the name of your apps in the apps.py config
> file conflicts with the core Django auth module and it could explains the
> second exception you get.

Yes. I know, that's why I added label="myauth". The problem is Django
1.6 allowed to call it 'auth'. Yes, it was our fault, we should have
called it accounts or something else.


> When you use a custom model in Django, the "migrate" command always create
> additional table for group and user_permissions but they are never filled in
> throught the admin site. According to me, Django creates these tables but
> they are useless. Why ? I don't really know... Maybe it's depending on the
> table creation process.

I'm sorry, I think I'm not following you. Are you saying myauth_myuser
are not used?

If I migrate this app to 1.8.5, and I add label="myauth" to avoid
collision with django.contrib.auth, I lose the users. They're in the
auth_myuser table, myauth_myuser is empty. If I create a user, the
user is created in myauth_myuser table.


> Sorry, my answer doesn't really help you but that's is my understanding of
> custom user model process at this time.

Don't worry. I'll make a dB dump, change the tables by hand and import
everything with label='myauth'. I was hoping there was a "Django way"
to fix this problem.

Thanks,
Norberto

syl pingus

unread,
Oct 15, 2015, 4:09:18 AM10/15/15
to django...@googlegroups.com
Hi,

Your table myauth_myuser is used to store your users. But Django create additional custom tables for groups and user_permissions related to the custom user model and which are useless and I don't uderrstand why. If somebody have an explanation or could tell me how to avoid the creation of the following tables using the migrate command it could be great : myauth_group - myauth_user_permissions

Sylvain.
--
> From: nbe...@gmail.com
> Date: Wed, 14 Oct 2015 23:13:52 -0300

> Subject: Re: Django 1.8: subclass AbstractUser, App 'auth' doesn't have a 'myuser' model.
> To: django...@googlegroups.com
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.

술욱

unread,
Dec 29, 2015, 6:39:50 PM12/29/15
to django...@googlegroups.com
I fixed this problem.


I renamed my app 'auth' to 'accounts' and then changed all the references of auth.something to accounts.something.

I renamed the tables and sequences (I use postgres):

auth_myuser -> accounts_myuser
auth_myuser_id_seq -> accounts_myuser_id_seq
etc.

I removed the southmigrations table.

Also changed django_content_type as described in the above post.

Then I upgraded to django<1.8, then <1.9, and lastly 1.9. I couldn't upgrade directly to 1.9.


Thanks all,

Regards,
Norberto

Reply all
Reply to author
Forward
0 new messages