Django custom user model in admin, relation “auth_user” does not exist

By | June 8, 2013

I have a custom user model as below:

class User(AbstractUser):
    subscribe_newsletters = models.BooleanField(default=True)
    old_id = models.IntegerField(null=True, blank=True)
    old_source = models.CharField(max_length=25, null=True, blank=True)

And using the builtin UserAdmin

admin.site.register(User, UserAdmin)

While editing the user record works fine, but when I add a user, I get the following error

Exception Value: 
relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...

After some digging around and found this https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms

The culprit is a function clean_uesrname inside UserCreationForm inside django.contrib.auth.forms.py
A few tickets have been created, but apparently the maintainers don’t think it’s a defect

https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20086

def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data[“username”]
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages[‘duplicate_username’])
The User in this file is directly referencing to the builtin user model.

To fix it, I created my custom forms

from models import User #you can use get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.admin import UserAdmin

class MyUserCreationForm(UserCreationForm):
    def clean_username(self):
        # Since User.username is unique, this check is redundant,
        # but it sets a nicer error message than the ORM. See #13147.
        username = self.cleaned_data["username"]
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

    class Meta(UserCreationForm.Meta):
        model = User

class MyUserAdmin(UserAdmin):  
    add_form = MyUserCreationForm   

admin.site.register(User,MyUserAdmin)

6 thoughts on “Django custom user model in admin, relation “auth_user” does not exist

  1. dvc

    Hi James, I’m not using a custom user model. I’m still getting this error. Do you have a clue as to why that could be?

    Reply
    1. James Lin Post author

      Sorry I cannot think of any obvious reason on top of my mind, you will have to show your code to see what might cause it, have you tried stackoverflow?

      Reply
    1. nik

      @dvc, what was the solution? I’m also getting this error without using a custom

      Reply
  2. nik

    @dvc, what was the solution? I’m also getting this error without using a custom user model.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.