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...

Migrate your app (the one with custom user model) first, and only then the rest:

$ ./manage.py makemigrations <your_app>
$ ./manage.py migrate
$ ./manage.py makemigrations
$ ./manage.py migrate

You can also control the order of migrations to make sure this happens automatically, see https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the-order-of-migrations

up vote 27 down vote accepted

After some digging around I 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_username 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
from django.contrib.auth import forms

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)

Or you can try monkey patching the original UserCreationForm to replace the User variable.

  • 1
    Nice catch. This should definitely be addressed considering the Django docs actually encourage you to to use these classes right out of the box when you extend the User model. – dustinfarris Oct 2 '13 at 14:13
  • 1
    Just the solution I'm looking for. – Charlesliam Jan 19 '14 at 13:28
  • 1
    It saved my day! – eneepo Jan 29 '14 at 8:34
  • Thanks! Just one small thing, your code example is missing an import: from django.contrib.auth import forms – Ludo Jan 31 '14 at 10:58
  • @Ludo thanks, added it. – James Lin Feb 2 '14 at 19:58

Django 1.8

If your app is not yet using migrations then this could also be the problem, as contrib.auth uses them. Enabling migrations for my app solved it for me.

$ ./manage.py makemigrations <my_app>
$ ./manage.py migrate
  • You saved my sanity. Apparently if you wipe migrations, you need to keep the migration folder and __init__.py in it, otherwise Django will fail to create the initial migration, and subsequently fail migrate. – Nelo Mitranim Aug 5 '15 at 15:42

This is due to migration is not run. This issue is resolved for me by running following command:

python manage.py syncdb

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.