2
votes

I'm using a custom user model myapp.MyUser in a Django 1.5 app. I updated my settings as documentation says to do, AUTH_USER_MODEL = "myapp.MyUser". As MyUser extends AbstractUser, I created the admin with this code:

from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

admin.site.register(get_user_model(), UserAdmin)

And it works fine, except the creation form. When I try to create a new user, the following exception is raised:

DatabaseError at /admin/core/user/add/
(1146, "Table 'mydatabase.auth_user' doesn't exist")

The full traceback can be found here.

Digging out Django's source code it looks like UserCreationForm - which is used by UserAdmin - references django's built-in auth.User directly, instead of use get_user_model.

Can it be the problem? Why everything references myapp.MyUser, including admin's auth and the change form, except the creation?

2
Did you run syncdb? - girasquid
Yep @girasquid, myapp_myuser was created, but auth_user won't, as expected. - Lucas Sampaio

2 Answers

2
votes

As you say, the UserCreationForm references auth.User directly. The Django docs on custom users and built in auth forms state that you must rewrite it for any custom user model.

UserCreationForm

Depends on the User model. Must be re-written for any custom user model.

There is a related ticket Ticket 20086, which was initially closed as invalid, because the user creation form is working as documented.

There is a full example on the same page, which shows you how to rewrite the forms and register a custom user model in the admin.

2
votes

If you don't mind to store your custom model in a table called auth_user you can simply set this in Meta and this solves auth_user table doesn't exist. All you need is:

class MyUser(AbstractUser):

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'
        db_table = 'auth_user'